How to use indent method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

text_renderer.py

Source: text_renderer.py Github

copy
1# -*- coding: utf-8 -*- #
2# Copyright 2015 Google Inc. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Cloud SDK markdown document text renderer."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from googlecloudsdk.core.console import console_attr
23from googlecloudsdk.core.document_renderers import renderer
24
25from six.moves import range  # pylint: disable=redefined-builtin
26
27
28class TextRenderer(renderer.Renderer):
29  """Renders markdown to text.
30
31  Attributes:
32    _attr: console_attr.ConsoleAttr object.
33    _bullet: List of bullet characters indexed by list level modulo #bullets.
34    _blank: True if the output already contains a blank line. Used to avoid
35      sequences of 2 or more blank lines in the output.
36    _csi_char: The first control sequence indicator character or None if control
37      sequences are not supported.
38    _fill: The number of characters in the current output line.
39    _ignore_width: True if the next output word should ignore _width.
40    _indent: List of left indentations in characters indexed by _level.
41    _level: The section or list level counting from 0.
42    _table: True if currently rendering a table.
43  """
44  INDENT = 4
45  SPLIT_INDENT = 2
46
47  class Indent(object):
48    """Hanging indent stack."""
49
50    def __init__(self):
51      self.indent = TextRenderer.INDENT
52      self.hanging_indent = self.indent
53
54  def __init__(self, *args, **kwargs):
55    super(TextRenderer, self).__init__(*args, **kwargs)
56    self._attr = console_attr.GetConsoleAttr()
57    self._blank = True
58    self._bullet = self._attr.GetBullets()
59    self._csi_char = self._attr.GetControlSequenceIndicator()
60    if self._csi_char:
61      self._csi_char = self._csi_char[0]
62    self._fill = 0
63    self._ignore_width = False
64    self._indent = [self.Indent()]
65    self._level = 0
66    self._table = False
67
68  def _Flush(self):
69    """Flushes the current collection of Fill() lines."""
70    self._ignore_width = False
71    if self._fill:
72      self._out.write('\n')
73      self._blank = False
74      self._fill = 0
75
76  def _SetIndent(self, level, indent=None, hanging_indent=None):
77    """Sets the markdown list level and indentations.
78
79    Args:
80      level: int, The desired markdown list level.
81      indent: int, The new indentation.
82      hanging_indent: int, The hanging indentation. This is subtracted from the
83        prevailing indent to decrease the indentation of the next input line
84        for this effect:
85            HANGING INDENT ON THE NEXT LINE
86               PREVAILING INDENT
87               ON SUBSEQUENT LINES
88    """
89    if self._level < level:
90      # The level can increase by 1 or more. Loop through each so that
91      # intervening levels are handled the same.
92      while self._level < level:
93        prev_level = self._level
94        self._level += 1
95        if self._level >= len(self._indent):
96          self._indent.append(self.Indent())
97        self._indent[self._level].indent = (
98            self._indent[prev_level].indent + indent)  # pytype: disable=wrong-arg-types
99        if (self._level > 1 and
100            self._indent[prev_level].hanging_indent ==
101            self._indent[prev_level].indent):
102          # Bump the indent by 1 char for nested indentation. Top level looks
103          # fine (aesthetically) without it.
104          self._indent[self._level].indent += 1
105        self._indent[self._level].hanging_indent = (
106            self._indent[self._level].indent)
107        if hanging_indent is not None:
108          # Adjust the hanging indent if specified.
109          self._indent[self._level].hanging_indent -= hanging_indent
110    else:
111      # Decreasing level just sets the indent stack level, no state to clean up.
112      self._level = level
113      if hanging_indent is not None:
114        # Change hanging indent on existing level.
115        self._indent[self._level].indent = (
116            self._indent[self._level].hanging_indent + hanging_indent)
117
118  def Example(self, line):
119    """Displays line as an indented example.
120
121    Args:
122      line: The example line text.
123    """
124    self._fill = self._indent[self._level].indent + self.INDENT
125    self._out.write(' ' * self._fill + line + '\n')
126    self._blank = False
127    self._fill = 0
128
129  def Fill(self, line):
130    """Adds a line to the output, splitting to stay within the output width.
131
132    This is close to textwrap.wrap() except that control sequence characters
133    don't count in the width computation.
134
135    Args:
136      line: The text line.
137    """
138    self._blank = True
139    for word in line.split():
140      if not self._fill:
141        self._fill = self._indent[self._level].indent - 1
142        self._out.write(' ' * self._fill)
143      width = self._attr.DisplayWidth(word)
144      if self._fill + width + 1 >= self._width and not self._ignore_width:
145        self._out.write('\n')
146        self._fill = self._indent[self._level].indent
147        self._out.write(' ' * self._fill)
148      else:
149        self._ignore_width = False
150        if self._fill:
151          self._fill += 1
152          self._out.write(' ')
153      self._fill += width
154      self._out.write(word)
155
156  def Finish(self):
157    """Finishes all output document rendering."""
158    self._Flush()
159    self.Font(out=self._out)
160
161  def Font(self, attr=None, out=None):
162    """Returns the font embellishment string for attr.
163
164    Args:
165      attr: None to reset to the default font, otherwise one of renderer.BOLD,
166        renderer.ITALIC, or renderer.CODE.
167      out: Writes tags to this stream if not None.
168
169    Returns:
170      The font embellishment string.
171    """
172    if attr is None:
173      self._font = 0
174    else:
175      mask = 1 << attr
176      self._font ^= mask
177    bold = self._font & ((1 << renderer.BOLD) | (1 << renderer.CODE))
178    italic = self._font & (1 << renderer.ITALIC)
179    code = self._attr.GetFontCode(bold=bold, italic=italic)
180    if out:
181      out.write(code)
182    return code
183
184  def Heading(self, level, heading):
185    """Renders a heading.
186
187    Args:
188      level: The heading level counting from 1.
189      heading: The heading text.
190    """
191    if level == 1 and heading.endswith('(1)'):
192      # Ignore man page TH.
193      return
194    self._Flush()
195    self.Line()
196    self.Font(out=self._out)
197    if level > 2:
198      self._out.write('  ' * (level - 2))
199    self._out.write(self.Font(renderer.BOLD) + heading +
200                    self.Font(renderer.BOLD) + '\n')
201    if level == 1:
202      self._out.write('\n')
203    self._blank = True
204    self._level = 0
205    self._rows = []
206
207  def Line(self):
208    """Renders a paragraph separating line."""
209    self._Flush()
210    if not self._blank:
211      self._blank = True
212      self._out.write('\n')
213
214  def List(self, level, definition=None, end=False):
215    """Renders a bullet or definition list item.
216
217    Args:
218      level: The list nesting level, 0 if not currently in a list.
219      definition: Bullet list if None, definition list item otherwise.
220      end: End of list if True.
221    """
222    self._Flush()
223    if not level:
224      self._level = level
225    elif end:
226      # End of list.
227      self._SetIndent(level)
228    elif definition is not None:
229      # Definition list item.
230      if definition:
231        self._SetIndent(level, indent=4, hanging_indent=3)
232        self._out.write(
233            ' ' * self._indent[level].hanging_indent + definition + '\n')
234      else:
235        self._SetIndent(level, indent=1, hanging_indent=0)
236        self.Line()
237    else:
238      # Bullet list item.
239      indent = 2 if level > 1 else 4
240      self._SetIndent(level, indent=indent, hanging_indent=2)
241      self._out.write(' ' * self._indent[level].hanging_indent +
242                      self._bullet[(level - 1) % len(self._bullet)])
243      self._fill = self._indent[level].indent + 1
244      self._ignore_width = True
245
246  def _SkipSpace(self, line, index):
247    """Skip space characters starting at line[index].
248
249    Args:
250      line: The string.
251      index: The starting index in string.
252
253    Returns:
254      The index in line after spaces or len(line) at end of string.
255    """
256    while index < len(line):
257      c = line[index]
258      if c != ' ':
259        break
260      index += 1
261    return index
262
263  def _SkipControlSequence(self, line, index):
264    """Skip the control sequence at line[index].
265
266    Args:
267      line: The string.
268      index: The starting index in string.
269
270    Returns:
271      The index in line after the control sequence or len(line) at end of
272      string.
273    """
274    n = self._attr.GetControlSequenceLen(line[index:])
275    if not n:
276      n = 1
277    return index + n
278
279  def _SkipNest(self, line, index, open_chars='[(', close_chars=')]'):
280    """Skip a [...] nested bracket group starting at line[index].
281
282    Args:
283      line: The string.
284      index: The starting index in string.
285      open_chars: The open nesting characters.
286      close_chars: The close nesting characters.
287
288    Returns:
289      The index in line after the nesting group or len(line) at end of string.
290    """
291    nest = 0
292    while index < len(line):
293      c = line[index]
294      index += 1
295      if c in open_chars:
296        nest += 1
297      elif c in close_chars:
298        nest -= 1
299        if nest <= 0:
300          break
301      elif c == self._csi_char:
302        index = self._SkipControlSequence(line, index)
303    return index
304
305  def _SplitWideSynopsisGroup(self, group, indent, running_width):
306    """Splits a wide SYNOPSIS section group string to self._out.
307
308    Args:
309      group: The wide group string to split.
310      indent: The prevailing left indent.
311      running_width: The width of the self._out line in progress.
312
313    Returns:
314      The running_width after the group has been split and written to self._out.
315    """
316    prev_delimiter = ' '
317    while group:
318      # Check split delimiters in order for visual emphasis.
319      for delimiter in (' | ', ' : ', ' ', ','):
320        part, _, remainder = group.partition(delimiter)
321        w = self._attr.DisplayWidth(part)
322        if ((running_width + len(prev_delimiter) + w) >= self._width or
323            prev_delimiter != ',' and delimiter == ','):
324          if delimiter != ',' and (indent +
325                                   self.SPLIT_INDENT +
326                                   len(prev_delimiter) +
327                                   w) >= self._width:
328            # The next delimiter may produce a smaller first part.
329            continue
330          if prev_delimiter == ',':
331            self._out.write(prev_delimiter)
332            prev_delimiter = ' '
333          if running_width != indent:
334            running_width = indent + self.SPLIT_INDENT
335            self._out.write('\n' + ' ' * running_width)
336        self._out.write(prev_delimiter + part)
337        running_width += len(prev_delimiter) + w
338        prev_delimiter = delimiter
339        group = remainder
340        break
341    return running_width
342
343  def Synopsis(self, line):
344    """Renders NAME and SYNOPSIS lines as a hanging indent.
345
346    Collapses adjacent spaces to one space, deletes trailing space, and doesn't
347    split top-level nested [...] or (...) groups. Also detects and does not
348    count terminal control sequences.
349
350    Args:
351      line: The NAME or SYNOPSIS text.
352    """
353    # Split the line into token, token | token, and [...] groups.
354    groups = []
355    i = self._SkipSpace(line, 0)
356    beg = i
357    while i < len(line):
358      c = line[i]
359      if c == ' ':
360        end = i
361        i = self._SkipSpace(line, i)
362        if i <= (len(line) - 1) and line[i] == '|' and line[i + 1] == ' ':
363          i = self._SkipSpace(line, i + 1)
364        else:
365          groups.append(line[beg:end])
366          beg = i
367      elif c in '[(':
368        i = self._SkipNest(line, i)
369      elif c == self._csi_char:
370        i = self._SkipControlSequence(line, i)
371      else:
372        i += 1
373    if beg < len(line):
374      groups.append(line[beg:])
375
376    # Output the groups.
377    indent = self._indent[0].indent - 1
378    running_width = indent
379    self._out.write(' ' * running_width)
380    indent += self.INDENT
381    for group in groups:
382      w = self._attr.DisplayWidth(group) + 1
383      if (running_width + w) >= self._width:
384        running_width = indent
385        self._out.write('\n' + ' ' * running_width)
386        if (running_width + w) >= self._width:
387          # The group is wider than the available width and must be split.
388          running_width = self._SplitWideSynopsisGroup(
389              group, indent, running_width)
390          continue
391      self._out.write(' ' + group)
392      running_width += w
393    self._out.write('\n\n')
394
395  def Table(self, line):
396    """Renders a table line.
397
398    Nested tables are not supported. The first call on a new table is:
399      Table(attributes)
400    the intermediate calls add the heading and data lines and the last call is:
401      Table(None)
402
403    Args:
404      line: A CSV table data line.
405    """
406    if line is None:
407      # TODO(b/31628974): Use resource_printer.TablePrinter().
408      if self._rows:
409        cols = len(self._rows[0])
410        width = [0 for _ in range(cols)]
411        for row in self._rows:
412          for i in range(cols - 1):
413            w = len(row[i])
414            if width[i] <= w:
415              width[i] = w + 1
416        for row in self._rows:
417          self._out.write(' ' * (self._indent[self._level].indent + 2))
418          for i in range(cols - 1):
419            self._out.write(row[i].ljust(width[i]))
420          self._out.write(row[-1] + '\n')
421        self._rows = []
422      self._table = False
423      self._out.write('\n')
424    elif not self._table:
425      self._table = True
426      self.Line()
427    else:
428      self._rows.append(line.split(','))
429
Full Screen

formatting.py

Source: formatting.py Github

copy
1from contextlib import contextmanager
2from .termui import get_terminal_size
3from .parser import split_opt
4from ._compat import term_len
5
6
7# Can force a width.  This is used by the test system
8FORCED_WIDTH = None
9
10
11def measure_table(rows):
12    widths = {}
13    for row in rows:
14        for idx, col in enumerate(row):
15            widths[idx] = max(widths.get(idx, 0), term_len(col))
16    return tuple(y for x, y in sorted(widths.items()))
17
18
19def iter_rows(rows, col_count):
20    for row in rows:
21        row = tuple(row)
22        yield row + ('',) * (col_count - len(row))
23
24
25def wrap_text(text, width=78, initial_indent='', subsequent_indent='',
26              preserve_paragraphs=False):
27    """A helper function that intelligently wraps text.  By default, it
28    assumes that it operates on a single paragraph of text but if the
29    `preserve_paragraphs` parameter is provided it will intelligently
30    handle paragraphs (defined by two empty lines).
31
32    If paragraphs are handled, a paragraph can be prefixed with an empty
33    line containing the ``\\b`` character (``\\x08``) to indicate that
34    no rewrapping should happen in that block.
35
36    :param text: the text that should be rewrapped.
37    :param width: the maximum width for the text.
38    :param initial_indent: the initial indent that should be placed on the
39                           first line as a string.
40    :param subsequent_indent: the indent string that should be placed on
41                              each consecutive line.
42    :param preserve_paragraphs: if this flag is set then the wrapping will
43                                intelligently handle paragraphs.
44    """
45    from ._textwrap import TextWrapper
46    text = text.expandtabs()
47    wrapper = TextWrapper(width, initial_indent=initial_indent,
48                          subsequent_indent=subsequent_indent,
49                          replace_whitespace=False)
50    if not preserve_paragraphs:
51        return wrapper.fill(text)
52
53    p = []
54    buf = []
55    indent = None
56
57    def _flush_par():
58        if not buf:
59            return
60        if buf[0].strip() == '\b':
61            p.append((indent or 0, True, '\n'.join(buf[1:])))
62        else:
63            p.append((indent or 0, False, ' '.join(buf)))
64        del buf[:]
65
66    for line in text.splitlines():
67        if not line:
68            _flush_par()
69            indent = None
70        else:
71            if indent is None:
72                orig_len = term_len(line)
73                line = line.lstrip()
74                indent = orig_len - term_len(line)
75            buf.append(line)
76    _flush_par()
77
78    rv = []
79    for indent, raw, text in p:
80        with wrapper.extra_indent(' ' * indent):
81            if raw:
82                rv.append(wrapper.indent_only(text))
83            else:
84                rv.append(wrapper.fill(text))
85
86    return '\n\n'.join(rv)
87
88
89class HelpFormatter(object):
90    """This class helps with formatting text-based help pages.  It's
91    usually just needed for very special internal cases, but it's also
92    exposed so that developers can write their own fancy outputs.
93
94    At present, it always writes into memory.
95
96    :param indent_increment: the additional increment for each level.
97    :param width: the width for the text.  This defaults to the terminal
98                  width clamped to a maximum of 78.
99    """
100
101    def __init__(self, indent_increment=2, width=None, max_width=None):
102        self.indent_increment = indent_increment
103        if max_width is None:
104            max_width = 80
105        if width is None:
106            width = FORCED_WIDTH
107            if width is None:
108                width = max(min(get_terminal_size()[0], max_width) - 2, 50)
109        self.width = width
110        self.current_indent = 0
111        self.buffer = []
112
113    def write(self, string):
114        """Writes a unicode string into the internal buffer."""
115        self.buffer.append(string)
116
117    def indent(self):
118        """Increases the indentation."""
119        self.current_indent += self.indent_increment
120
121    def dedent(self):
122        """Decreases the indentation."""
123        self.current_indent -= self.indent_increment
124
125    def write_usage(self, prog, args='', prefix='Usage: '):
126        """Writes a usage line into the buffer.
127
128        :param prog: the program name.
129        :param args: whitespace separated list of arguments.
130        :param prefix: the prefix for the first line.
131        """
132        usage_prefix = '%*s%s ' % (self.current_indent, prefix, prog)
133        text_width = self.width - self.current_indent
134
135        if text_width >= (term_len(usage_prefix) + 20):
136            # The arguments will fit to the right of the prefix.
137            indent = ' ' * term_len(usage_prefix)
138            self.write(wrap_text(args, text_width,
139                                 initial_indent=usage_prefix,
140                                 subsequent_indent=indent))
141        else:
142            # The prefix is too long, put the arguments on the next line.
143            self.write(usage_prefix)
144            self.write('\n')
145            indent = ' ' * (max(self.current_indent, term_len(prefix)) + 4)
146            self.write(wrap_text(args, text_width,
147                                 initial_indent=indent,
148                                 subsequent_indent=indent))
149
150        self.write('\n')
151
152    def write_heading(self, heading):
153        """Writes a heading into the buffer."""
154        self.write('%*s%s:\n' % (self.current_indent, '', heading))
155
156    def write_paragraph(self):
157        """Writes a paragraph into the buffer."""
158        if self.buffer:
159            self.write('\n')
160
161    def write_text(self, text):
162        """Writes re-indented text into the buffer.  This rewraps and
163        preserves paragraphs.
164        """
165        text_width = max(self.width - self.current_indent, 11)
166        indent = ' ' * self.current_indent
167        self.write(wrap_text(text, text_width,
168                             initial_indent=indent,
169                             subsequent_indent=indent,
170                             preserve_paragraphs=True))
171        self.write('\n')
172
173    def write_dl(self, rows, col_max=30, col_spacing=2):
174        """Writes a definition list into the buffer.  This is how options
175        and commands are usually formatted.
176
177        :param rows: a list of two item tuples for the terms and values.
178        :param col_max: the maximum width of the first column.
179        :param col_spacing: the number of spaces between the first and
180                            second column.
181        """
182        rows = list(rows)
183        widths = measure_table(rows)
184        if len(widths) != 2:
185            raise TypeError('Expected two columns for definition list')
186
187        first_col = min(widths[0], col_max) + col_spacing
188
189        for first, second in iter_rows(rows, len(widths)):
190            self.write('%*s%s' % (self.current_indent, '', first))
191            if not second:
192                self.write('\n')
193                continue
194            if term_len(first) <= first_col - col_spacing:
195                self.write(' ' * (first_col - term_len(first)))
196            else:
197                self.write('\n')
198                self.write(' ' * (first_col + self.current_indent))
199
200            text_width = max(self.width - first_col - 2, 10)
201            lines = iter(wrap_text(second, text_width).splitlines())
202            if lines:
203                self.write(next(lines) + '\n')
204                for line in lines:
205                    self.write('%*s%s\n' % (
206                        first_col + self.current_indent, '', line))
207            else:
208                self.write('\n')
209
210    @contextmanager
211    def section(self, name):
212        """Helpful context manager that writes a paragraph, a heading,
213        and the indents.
214
215        :param name: the section name that is written as heading.
216        """
217        self.write_paragraph()
218        self.write_heading(name)
219        self.indent()
220        try:
221            yield
222        finally:
223            self.dedent()
224
225    @contextmanager
226    def indentation(self):
227        """A context manager that increases the indentation."""
228        self.indent()
229        try:
230            yield
231        finally:
232            self.dedent()
233
234    def getvalue(self):
235        """Returns the buffer contents."""
236        return ''.join(self.buffer)
237
238
239def join_options(options):
240    """Given a list of option strings this joins them in the most appropriate
241    way and returns them in the form ``(formatted_string,
242    any_prefix_is_slash)`` where the second item in the tuple is a flag that
243    indicates if any of the option prefixes was a slash.
244    """
245    rv = []
246    any_prefix_is_slash = False
247    for opt in options:
248        prefix = split_opt(opt)[0]
249        if prefix == '/':
250            any_prefix_is_slash = True
251        rv.append((len(prefix), opt))
252
253    rv.sort(key=lambda x: x[0])
254
255    rv = ', '.join(x[1] for x in rv)
256    return rv, any_prefix_is_slash
257
Full Screen

matdocparser.py

Source: matdocparser.py Github

copy
1#!/usr/bin/python
2# file: matdocparser.py
3# author: Andrea Vedaldi
4# description: Utility to format MATLAB comments.
5
6# Copyright (C) 2014-15 Andrea Vedaldi.
7# All rights reserved.
8#
9# This file is part of the VLFeat library and is made available under
10# the terms of the BSD license (see the COPYING file).
11
12"""
13MatDocParser is an interpreter for the MatDoc format. This is a simplified and
14stricter version of Markdown suitable to commenting MATLAB functions. the format
15is easily understood from an example:
16
17A paragraph starts on a new line.
18And continues on following lines.
19
20Indenting with a whitespace introduces a verbatim code section:
21
22   Like this
23    This continues it
24
25Different paragraphs are separated by blank lines.
26
27* The *, -, + symbols at the beginning of a line introduce a list.
28  Which can be continued on follwing paragraphs by proper indentation.
29
30  Multiple paragraphs in a list item are also supported.
31
32* This is the second item of the same list.
33
34It is also possible to have definition lists such as
35
36Term1:: Short description 2
37   Longer explanation.
38
39   Behaves like a list item.
40
41Term2:: Short description 2
42Term3:: Short description 3
43  Longer explanations are optional.
44
45# Lines can begin with # to denote a title
46## Is a smaller title
47"""
48
49import sys
50import os
51import re
52
53__mpname__           = 'MatDocParser'
54__version__          = '1.0-beta25'
55__date__             = '2015-09-20'
56__description__      = 'MatDoc MATLAB inline function description interpreter.'
57__long_description__ = __doc__
58__license__          = 'BSD'
59__author__           = 'Andrea Vedaldi'
60
61# --------------------------------------------------------------------
62# Input line types (terminal symbols)
63# --------------------------------------------------------------------
64
65# Terminal symbols are organized in a hierarchy. Each line in the
66# input document is mapped to leaf in this hierarchy, representing
67# the type of line detected.
68
69class Symbol(object):
70    indent = None
71    def isa(self, classinfo, indent = None):
72        return isinstance(self, classinfo) and \
73            (indent is None or self.indent == indent)
74    def __str__(self, indent = 0):
75        if self.indent is not None: x = "%d" % self.indent
76        else: x = "*"
77        return " "*indent + "%s(%s)" % (self.__class__.__name__, x)
78
79# Terminal symbols
80# Note that PL, BH, DH are all subclasses of L; the fields .text and .indent
81# have the same meaning for all of them.
82class Terminal(Symbol): pass
83class EOF (Terminal): pass # end-of-file
84class B (Terminal): pass # blank linke
85class L (Terminal): # non-empty line: '<" "*indent><text>'
86    text = ""
87    def __str__(self, indent = 0):
88        return "%s: %s" % (super(L, self).__str__(indent), self.text)
89class PL (L): pass # regular line
90class BH (L): # bullet: a line of type '  * <inner_text>'
91    inner_indent = None
92    inner_text = None
93    bullet = None
94class DH (L):  # description: a line of type ' <description>::<inner_text>'
95    inner_text = None
96    description = None
97    def __str__(self, indent = 0):
98        return "%s: '%s' :: '%s'" % (super(L, self).__str__(indent),
99                           self.description, self.inner_text)
100class SL (L): # section: '<#+><text>'
101    section_level = 0
102    inner_text = None
103    def __str__(self, indent = 0):
104        return "%s: %s" % (super(L, self).__str__(indent), self.inner_text)
105
106# A lexer object: parse lines of the input document into terminal symbols
107class Lexer(object):
108    def __init__(self, lines):
109        self.lines = lines
110        self.pos = -1
111
112    def next(self):
113        self.pos = self.pos + 1
114        # no more
115        if self.pos > len(self.lines)-1:
116            x = EOF()
117            return x
118        line = self.lines[self.pos]
119        # a blank line
120        match = re.match(r"\s*\n?$", line) ;
121        if match:
122            return B()
123        # a line of type '  <#+><inner_text>'
124        match = re.match(r"(\s*)(#+)(.*)\n?$", line)
125        if match:
126            x = SL()
127            x.indent = len(match.group(1))
128            x.section_level = len(match.group(2))
129            x.inner_text = match.group(3)
130            #print x.indent, x.section_level, x.inner_text
131            return x
132        # a line of type '  <content>::<inner_text>'
133        match = re.match(r"(\s*)(.*)::(.*)\n?$", line)
134        if match:
135            x = DH()
136            x.indent = len(match.group(1))
137            x.description = match.group(2)
138            x.inner_text = match.group(3)
139            x.text = x.description + "::" + x.inner_text
140            return x
141        # a line of type '  * <inner_contet>'
142        match = re.match(r"(\s*)([-\*+]\s*)(\S.*)\n?$", line)
143        if match:
144            x = BH()
145            x.indent = len(match.group(1))
146            x.bullet = match.group(2)
147            x.inner_indent = x.indent + len(x.bullet)
148            x.inner_text = match.group(3)
149            x.text = x.bullet + x.inner_text
150            return x
151        # a line of the type  '   <content>'
152        match = re.match(r"(\s*)(\S.*)\n?$", line)
153        if match:
154            x = PL()
155            x.indent = len(match.group(1))
156            x.text = match.group(2)
157            return x
158
159# --------------------------------------------------------------------
160# Non-terminal
161# --------------------------------------------------------------------
162
163# DIVL is a consecutive list of blocks with the same indent and/or blank
164# lines.
165#
166# DIVL(indent) -> (B | SL(indent) | P(indent) | V(indent) |
167#                  BL(indent) | DL(indent))+
168#
169# S(indent) -> SL(indent)
170#
171# A P(indent) is a paragraph, a list of regular lines indentent by the
172# same amount.
173#
174# P(indent) -> PL(indent)+
175#
176# A V(indent) is a verbatim (code) block. It contains text lines and blank
177# lines that have indentation strictly larger than `indent`:
178#
179# V(indent) -> L(i) (B | L(j), j > indent)+, for all i > indent
180#
181# A DL(indent) is a description list:
182#
183# DL(indent) -> DH(indent) DIVL(i)*,  i > indent
184#
185# A BL(indent) is a bullet list. It contains bullet list items, namely
186# a sequence of special DIVL_BH(indent,inner_indent) whose first block
187# is a paragaraph P_BH(indent,inner_indent) whose first line is a
188# bullet header BH(indent,innner_indent). Here the bullet identation
189# inner_indent is obtained as the inner_indent of the
190# BH(indent,inner_indent) symbol. Formalising this with grammar rules
191# is verbose; instead we use the simple `hack' of defining
192#
193# BL(indent) -> (DIVL(inner_indent))+
194#
195# where DIVL(inner_indent) are regular DIVL, obtaine after replacing
196# the bullet header line BH with a standard paragraph line PL.
197
198class NonTerminal(Symbol):
199    children = []
200    def __init__(self, *args):
201        self.children = list(args)
202    def __str__(self, indent = 0):
203        s = " "*indent + super(NonTerminal, self).__str__() + "\n"
204        for c in self.children:
205            s += c.__str__(indent + 2) + "\n"
206        return s[:-1]
207
208class S(NonTerminal): pass
209class DIVL(NonTerminal): pass
210class DIV(NonTerminal): pass
211class BL(NonTerminal): pass
212class DL(NonTerminal): pass
213class DI(NonTerminal): pass
214class P(DIV): pass
215class V(DIV): pass
216
217# --------------------------------------------------------------------
218class Parser(object):
219    lexer = None
220    stack = []
221    lookahead = None
222
223    def shift(self):
224        if self.lookahead:
225            self.stack.append(self.lookahead)
226        self.lookahead = self.lexer.next()
227
228    def reduce(self, X, n, indent = None):
229        #print "reducing %s with %d" % (S.__name__, n)
230        x = X(*self.stack[-n:])
231        del self.stack[-n:]
232        x.indent = indent
233        self.stack.append(x)
234        return x
235
236    def parse(self, lexer):
237        self.lexer = lexer
238        self.stack = []
239        while True:
240            self.lookahead = self.lexer.next()
241            if not self.lookahead.isa(B): break
242        self.parse_DIVL(self.lookahead.indent)
243        return self.stack[0]
244
245    def parse_SL(self, indent):
246        self.shift()
247        self.reduce(S, 1, indent)
248
249    def parse_P(self, indent):
250        i = 0
251        if indent is None: indent = self.lookahead.indent
252        while self.lookahead.isa(PL, indent):
253            self.shift()
254            i = i + 1
255        self.reduce(P, i, indent)
256
257    def parse_V(self, indent):
258        i = 0
259        while (self.lookahead.isa(L) and self.lookahead.indent > indent) or \
260              (self.lookahead.isa(B)):
261            self.shift()
262            i = i + 1
263        self.reduce(V, i, indent)
264
265    def parse_DIV_helper(self, indent):
266        if self.lookahead.isa(SL, indent):
267            self.parse_SL(indent)
268        elif self.lookahead.isa(PL, indent):
269            self.parse_P(indent)
270        elif self.lookahead.isa(L) and (self.lookahead.indent > indent):
271            self.parse_V(indent)
272        elif self.lookahead.isa(BH, indent):
273            self.parse_BL(indent)
274        elif self.lookahead.isa(DH, indent):
275            self.parse_DL(indent)
276        elif self.lookahead.isa(B):
277            self.shift()
278        else:
279            return False
280        # leaves with B, P(indent), V(indent), BL(indent) or DL(indent)
281        return True
282
283    def parse_BI_helper(self, indent):
284        x = self.lookahead
285        if not x.isa(BH, indent): return False
286        indent = x.inner_indent
287        self.lookahead = PL()
288        self.lookahead.text = x.inner_text
289        self.lookahead.indent = indent
290        self.parse_DIVL(indent)
291        # leaves with DIVL(inner_indent) where inner_indent was
292        # obtained from the bullet header symbol
293        return True
294
295    def parse_BL(self, indent):
296        i = 0
297        while self.parse_BI_helper(indent): i = i + 1
298        if i == 0: print "Error", sys.exit(1)
299        self.reduce(BL, i, indent)
300
301    def parse_DI_helper(self, indent):
302        if not self.lookahead.isa(DH, indent): return False
303        self.shift()
304        if self.lookahead.indent > indent:
305            self.parse_DIVL(self.lookahead.indent)
306            self.reduce(DI, 2, indent)
307        else:
308            self.reduce(DI, 1, indent)
309        return True
310
311    def parse_DL(self, indent):
312        i = 0
313        while self.parse_DI_helper(indent): i = i + 1
314        if i == 0: print "Error", sys.exit(1)
315        self.reduce(DL, i, indent)
316
317    def parse_DIVL(self, indent = None):
318        i = 0
319        while self.parse_DIV_helper(indent):
320            if indent is None: indent = self.stack[-1].indent
321            i = i + 1
322        self.reduce(DIVL, i, indent)
323
324if __name__ == '__main__':
325    str="""
326
327Some text describing a MATLAB function F().
328The function F() does nothing.
329
330It has the following options:
331
332CarryOn:: True
333  Keep doing nothing for the time being.
334
335Stop:: 'here'
336  Stop doing whathever here. Example:
337
338    % call the function
339    f('stop', 'there')
340
341    % contemplate the results
342
343So in short we conclude that:
344
345* This does nothing
346*   It could do something,
347    but still does not.
348
349   #
350
351See also: hope for the best.
352
353# Section number one
354
355Bla
356
357## More Sect
358### Even more
359
360blo
361"""
362    parser = Parser()
363    lexer = Lexer(str.split('\n'))
364    tree = parser.parse(lexer)
365    print tree
366
367
Full Screen

hdf5_IO.py

Source: hdf5_IO.py Github

copy
1from HartreeParticleDSL.c_types import c_int, c_double, c_float, c_int64_t, \
2                                       c_int32_t, c_int8_t, c_bool
3from HartreeParticleDSL.IO_modules.base_IO_module.IO_module import IO_Module
4from HartreeParticleDSL.backends.C_AOS.C_AOS import C_AOS
5from HartreeParticleDSL.backends.C_AOS.C_AOS_IO_Mixin import C_AOS_IO_Mixin
6from HartreeParticleDSL.backends.FDPS_backend.FDPS import FDPS
7from HartreeParticleDSL.backends.FDPS_backend.FDPS_IO_Mixin import FDPS_IO_Mixin
8
9class HDF5_IO(IO_Module, C_AOS_IO_Mixin, FDPS_IO_Mixin):
10    '''Implementation of the HDF5 IO Module'''
11    type_map = {c_int : "H5T_STD_I32LE",
12                c_double : "H5T_NATIVE_DOUBLE",
13                c_float : "H5T_NATIVE_FLOAT",
14                c_int64_t : "H5T_STD_I64LE",
15                c_int32_t : "H5T_STD_I32LE",
16                c_int8_t : "H5T_STD_I8LE",
17                c_bool : "H5T_NATIVE_HBOOL"}
18
19    def __init__(self, indent=4):
20        super().__init__()
21        self._inputs = {}
22        self._outputs = {}
23        self._indent = indent
24        self._current_indent = self._indent
25
26    def add_input(self, hdf_input, particle_input):
27        self._inputs[hdf_input] = particle_input
28
29    def add_output(self, hdf_output, particle_output):
30        self._outputs[hdf_output] = particle_output
31
32    def indent(self):
33        return " " * self._current_indent
34
35    def increment_indent(self):
36        self._current_indent = self._current_indent + self._indent
37
38    def decrement_indent(self):
39        self._current_indent = self._current_indent - self._indent
40
41    # C_AOS_IO_Mixin functions
42
43    def get_includes_c(self):
44        '''
45        :returns: The includes required for this IO module.
46        :rtype: List of str
47
48        '''
49        includes = []
50        includes.append("<stdlib.h>")
51        includes.append("\"hdf5.h\"")
52        return includes
53
54    def gen_code_c(self, part_type):
55        '''
56        Returns the C code required for this IO module.
57
58        :param part_type: The particle type used in the system.
59        :type part_type: Particle
60        '''
61        code = ""
62
63        if len(self._inputs) > 0:
64            # Create the hdf5_input function
65            code = code + self.indent() + "struct part* hdf5_input(const char* filename, struct config_type* config){\n"
66            self.increment_indent()
67            # Check that the asked for fields are in the file, and pull out the number of particles
68            code = code + self.indent() + "hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);\n"
69            code = code + self.indent() + "if( file_id < 0 ){\n"
70            self.increment_indent()
71            code = code + self.indent() + "printf(\"Failed to open %s\\n\", filename);\n"
72            code = code + self.indent() + "exit(1);\n"
73            self.decrement_indent()
74            code = code + self.indent() + "}\n"
75            for key in self._inputs.keys():
76                code = code + self.indent() + "hid_t {0}_test_var = H5Dopen2(file_id, \"{0}\", H5P_DEFAULT);\n".format(key)
77                code = code + self.indent() + "if( {0}_test_var < 0)".format(key) + "{\n"
78                self.increment_indent()
79                code = code + self.indent() + "printf(\"Failed to find dataset {0}\\n\");\n".format(key)
80                code = code + self.indent() + "exit(1);\n"
81                self.decrement_indent()
82                code = code + self.indent() + "}\n"
83                code = code + self.indent() + "H5Dclose({0}_test_var);\n".format(key)
84            # All of the fields are in the HDF5 file as requested, fetch the particle count.
85            code = code + self.indent() + "hid_t part_count_var = H5Dopen2(file_id, \"{0}\", H5P_DEFAULT);\n".format(list(self._inputs.keys())[0])
86            code = code + self.indent() + "hid_t space = H5Dget_space(part_count_var);\n"
87            code = code + self.indent() + "int ndims = H5Sget_simple_extent_ndims(space);\n"
88            # Can't handle multi dimensional datasets for now
89            code = code + self.indent() + "if( ndims != 1 ){\n"
90            self.increment_indent()
91            code = code + self.indent() + "printf(\"Don't yet support multidimensional datasets.\\n\");\n".format(key)
92            code = code + self.indent() + "exit(1);\n"
93            self.decrement_indent()
94            code = code + self.indent() + "}\n"
95            # Get the num parts
96            code = code + self.indent() + "hsize_t dims[1];\n"
97            code = code + self.indent() + "H5Sget_simple_extent_dims(space, dims, NULL);\n"
98            code = code + self.indent() + "int num_parts = dims[0];\n"
99
100            # Init the data structures
101            code = code + self.indent() + "struct part *parts = (struct part*) malloc(sizeof(struct part) * num_parts);\n"
102            code = code + self.indent() + "config->space.nparts = num_parts;\n\n"
103
104            # Read the values from the file into the particles
105            code = code + self.indent() + "hsize_t shape[2], offsets[2];\n"
106            code = code + self.indent() + "int rank = 2;\n"
107            code = code + self.indent() + "shape[0] = num_parts;\n"
108            code = code + self.indent() + "shape[1] = 1;\n"
109            code = code + self.indent() + "offsets[0] = 0;\n"
110            code = code + self.indent() + "offsets[1] = 0;\n"
111            code = code + self.indent() + "hid_t memspace = H5Screate_simple(rank, shape, NULL);\n"
112            code = code + self.indent() + "hid_t filespace;\n"
113
114            for key in self._inputs.keys():
115                code = code + self.indent() + "hid_t {0}_read_var = H5Dopen2(file_id, \"{0}\", H5P_DEFAULT);\n".format(key)
116                code = code + self.indent() + "filespace = H5Dget_space({0}_read_var);\n".format(key)
117                code = code + self.indent() + "H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsets, NULL, shape, NULL);\n"
118                # Find the type of the variable from the particle structure
119                part_elem = self._inputs[key]
120                elem_type = None
121                h5_type = None
122                if part_elem.startswith("core_part"):
123                    temp_string = part_elem.replace("core_part.", "")
124                    if temp_string == "position.x" or temp_string == "position[0]":
125                        part_elem = "core_part.position[0]"
126                        elem_type = c_double
127                    elif temp_string == "position.y" or temp_string == "position[1]":
128                        part_elem = "core_part.position[1]"
129                        elem_type = c_double
130                    elif temp_string == "position.z" or temp_string == "position[2]":
131                        part_elem = "core_part.position[2]"
132                        elem_type = c_double
133                    elif temp_string == "velocity.x" or temp_string == "velocity[0]":
134                        part_elem = "core_part.velocity[0]"
135                        elem_type = c_double
136                    elif temp_string == "velocity.y" or temp_string == "velocity[1]":
137                        part_elem = "core_part.velocity[1]"
138                        elem_type = c_double
139                    elif temp_string == "velocity.z" or temp_string == "velocity[2]":
140                        part_elem = "core_part.velocity[2]"
141                        elem_type = c_double
142                elif part_elem.startswith("neighbour_part"):
143                    pass
144                else:
145                    elem_type = part_type.particle_type[part_elem]['type']
146                h5_type = HDF5_IO.type_map.get(elem_type, None)
147                if h5_type is None:
148                    assert False
149                elem_type = C_AOS._type_map[elem_type]
150                code = code + self.indent() + "{0}* {1}_temp_array = ({0}*) malloc(sizeof({0}) * num_parts);\n".format(elem_type, key)
151                code = code + self.indent() + "H5Dread({0}_read_var, {1}, memspace, filespace, H5P_DEFAULT, {0}_temp_array);\n".format(key, h5_type)
152                code = code + self.indent() + "for( int i = 0; i < num_parts; i++){\n"
153                self.increment_indent()
154                code = code + self.indent() + "parts[i].{0} = {1}_temp_array[i];\n".format(part_elem, key)
155                self.decrement_indent()
156                code = code + self.indent() + "}\n"
157                code = code + self.indent() + "free({0}_temp_array);\n".format(key)
158                code = code + self.indent() + "H5Dclose({0}_read_var);\n".format(key)
159
160            # All data read in
161            code = code + "\n" + self.indent() + "H5Fclose(file_id);\n"
162
163            code = code + self.indent() + "return parts;\n"
164            self.decrement_indent()
165            code = code + self.indent() + "}\n\n"
166
167        if len(self._outputs) > 0:
168            # Create the hdf5 output function
169            code = code + "\n"
170            code = code + self.indent() + "void hdf5_output(struct part* parts, struct config_type* config, const char* filename){\n"
171            self.increment_indent()
172            # Create the HDF5 file
173            code = code + self.indent() + "hid_t file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);\n"
174            code = code + self.indent() + "if( file_id < 0 ){\n"
175            self.increment_indent()
176            code = code + self.indent() + "printf(\"Couldn't create HDF5 file\\n\");\n"
177            code = code + self.indent() + "exit(1);\n"
178            self.decrement_indent()
179            code = code + self.indent() + "}\n\n"
180            # Setup dimensions of outputs
181            code = code + self.indent() + "hsize_t dims[1];\n"
182            code = code + self.indent() + "dims[0] = config->space.nparts;\n"
183            code = code + self.indent() + "hid_t dim = H5Screate_simple(1, dims, NULL);\n"
184
185            # Create dataset and output the requested values
186            for key in self._outputs.keys():
187                # Find the type of the variable from the particle structure
188                part_elem = self._outputs[key]
189                elem_type = None
190                h5_type = None
191                if part_elem.startswith("core_part"):
192                    temp_string = part_elem.replace("core_part.", "")
193                    if temp_string == "position.x" or temp_string == "position[0]":
194                        part_elem = "core_part.position[0]"
195                        elem_type = c_double
196                    elif temp_string == "position.y" or temp_string == "position[1]":
197                        part_elem = "core_part.position[1]"
198                        elem_type = c_double
199                    elif temp_string == "position.z" or temp_string == "position[2]":
200                        part_elem = "core_part.position[2]"
201                        elem_type = c_double
202                    elif temp_string == "velocity.x" or temp_string == "velocity[0]":
203                        part_elem = "core_part.velocity[0]"
204                        elem_type = c_double
205                    elif temp_string == "velocity.y" or temp_string == "velocity[1]":
206                        part_elem = "core_part.velocity[1]"
207                        elem_type = c_double
208                    elif temp_string == "velocity.z" or temp_string == "velocity[2]":
209                        part_elem = "core_part.velocity[2]"
210                        elem_type = c_double
211                elif part_elem.startswith("neighbour_part"):
212                    pass
213                else:
214                    elem_type = part_type.particle_type[part_elem]['type']
215                h5_type = HDF5_IO.type_map.get(elem_type, None)
216                if h5_type is None:
217                    assert False
218                elem_type = C_AOS._type_map[elem_type]
219                code = code + self.indent() + "hid_t {0}_output_field = H5Dcreate2(file_id, \"{0}\", {1}, dim, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);\n".format(
220                        key, h5_type)
221                # TODO Check if we successfully made the dataset
222                code = code + self.indent() + "{0}* {1}_output_array = ({0} *) malloc(sizeof({0}) * config->space.nparts);\n".format(elem_type, key)
223                code = code + self.indent() + "for( int i = 0; i < config->space.nparts; i++){\n"
224                self.increment_indent()
225                code = code + self.indent() + "{0}_output_array[i] = parts[i].{1};\n".format(key, part_elem)
226                self.decrement_indent()
227                code = code + self.indent() + "}\n\n"
228                code = code + self.indent() + "H5Dwrite({0}_output_field, {1}, H5S_ALL, H5S_ALL, H5P_DEFAULT, {0}_output_array);\n".format(key, h5_type)
229                code = code + self.indent() + "free({0}_output_array);\n".format(key)
230                code = code + self.indent() + "H5Dclose({0}_output_field);\n".format(key)
231
232
233            # End of output function
234            code = code + "\n" + self.indent() + "H5Fclose(file_id);\n"
235            self.decrement_indent()
236            code = code + self.indent() + "}\n\n"
237
238        return code
239
240    def call_input_c(self, part_count, filename):
241        '''
242        Returns the C call required to use this IO module for input.
243
244        :returns: The function call used for this module.
245        :rtype: str
246        '''
247
248        return f"hdf5_input({filename}, config);"
249
250    def call_output_c(self, part_count, filename):
251        '''
252        Returns the C call required to use this IO module for output.
253
254        :returns: The function call used for this module.
255        :rtype: str
256        '''
257
258        return f"hdf5_output(parts, config, {filename});"
259
260    # FDPS_IO_Mixin functions
261    def gen_code_fdps(self, part_type):
262        '''
263        Returns the FDPS C++ code required for this IO module.
264
265        :param part_type: The particle type used in the system.
266        :type part_type: Particle
267        '''
268        code = ""
269
270        if len(self._inputs) > 0:
271            # Create the hdf5_input function
272            code = code + self.indent() + "void hdf5_input(PS::ParticleSystem<FullParticle>& particle_system, config_type& config, const char *filename){\n"
273            self.increment_indent()
274            code = code + self.indent() + "hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);\n"
275            code = code + self.indent() + "if( file_id < 0 ){\n"
276            self.increment_indent()
277            code = code + self.indent() + "printf(\"Failed to open %s\\n\", filename);\n"
278            code = code + self.indent() + "exit(1);\n"
279            self.decrement_indent()
280            code = code + self.indent() + "}\n"
281            for key in self._inputs.keys():
282                code = code + self.indent() + "hid_t {0}_test_var = H5Dopen2(file_id, \"{0}\", H5P_DEFAULT);\n".format(key)
283                code = code + self.indent() + "if( {0}_test_var < 0)".format(key) + "{\n"
284                self.increment_indent()
285                code = code + self.indent() + "printf(\"Failed to find dataset {0}\\n\");\n".format(key)
286                code = code + self.indent() + "exit(1);\n"
287                self.decrement_indent()
288                code = code + self.indent() + "}\n"
289                code = code + self.indent() + "H5Dclose({0}_test_var);\n".format(key)
290            # All of the fields are in the HDF5 file as requested, fetch the particle count.
291            code = code + self.indent() + "hid_t part_count_var = H5Dopen2(file_id, \"{0}\", H5P_DEFAULT);\n".format(list(self._inputs.keys())[0])
292            code = code + self.indent() + "hid_t space = H5Dget_space(part_count_var);\n"
293            code = code + self.indent() + "int ndims = H5Sget_simple_extent_ndims(space);\n"
294            # Can't handle multi dimensional datasets for now
295            code = code + self.indent() + "if( ndims != 1 ){\n"
296            self.increment_indent()
297            code = code + self.indent() + "printf(\"Don't yet support multidimensional datasets.\\n\");\n".format(key)
298            code = code + self.indent() + "exit(1);\n"
299            self.decrement_indent()
300            code = code + self.indent() + "}\n"
301            # Get the num parts
302            code = code + self.indent() + "hsize_t[1] dims;\n"
303            code = code + self.indent() + "H5Sget_simple_extent_dims(space, dims, NULL);\n"
304            code = code + self.indent() + "int num_parts = dims[0];\n"
305       
306            # Initialise the particle system
307            code = code + self.indent() + "particle_system.initialize();\n"
308            # Single node for now.
309            code = code + self.indent() + "particle_system.setNumberOfParticleLocal(num_parts);\n"
310            code = code + self.indent() + "config.space.nparts = num_parts;\n"
311            # Setup the domain
312            code = code + self.indent() + "PS::DomainInfo dinfo;\n"
313            # Assume box sizes is setup for now.
314            code = code + self.indent() + "dinfo.initialize();\n"
315            # Assume periodic only for now.
316            code = code + self.indent() + "dinfo.setBoundaryCondition ( PS::BOUNDARY_CONDITION_PERIODIC_XYZ );\n"
317            code = code + self.indent() + \
318            '''dinfo.setPosRootDomain(PS::F64vec(config.space.box_dims.x_min,
319                                         config.space.box_dims.y_min,
320                                         config.space.box_dims.z_min),
321                              PS::F64vec(config.space.box_dims.x_max,
322                                         config.space.box_dims.y_max,
323                                         config.space.box_dims.z_max));\n\n'''
324
325            # Read the values from the file into the particles
326            code = code + self.indent() + "hsize_t shape[2], offsets[2];\n"
327            code = code + self.indent() + "int rank = 2;\n"
328            code = code + self.indent() + "shape[0] = num_parts;\n"
329            code = code + self.indent() + "shape[1] = 1;\n"
330            code = code + self.indent() + "offsets[0] = 0;\n"
331            code = code + self.indent() + "offsets[1] = 0;\n"
332            code = code + self.indent() + "hid_t memspace = H5Screate_simple(rank, shape, NULL);\n"
333            code = code + self.indent() + "hid_t filespace;\n"
334            code = code + self.indent() + "H5Sselect_hyperslab(h_filespace, H5S_SELECT_SET, offsets, NULL, shape, NULL);\n"
335            for key in self._inputs.keys():
336                code = code + self.indent() + "hid_t {0}_read_var = H5Dopen2(file_id, \"{0}\", H5P_DEFAULT);\n".format(key)
337                code = code + self.indent() + "filespace = H5Dget_space({0}_read_var);\n".format(key)
338                code = code + self.indent() + "H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsets, NULL, shape, NULL);\n"
339                # Find the type of the variable from the particle structure
340                part_elem = self._inputs[key]
341                elem_type = None
342                h5_type = None
343                if part_elem.startswith("core_part"):
344                    temp_string = part_elem.replace("core_part.", "")
345                    if temp_string == "position.x" or temp_string == "position[0]":
346                        part_elem = "core_part.position.x"
347                        elem_type = c_double
348                    elif temp_string == "position.y" or temp_string == "position[1]":
349                        part_elem = "core_part.position.y"
350                        elem_type = c_double
351                    elif temp_string == "position.z" or temp_string == "position[2]":
352                        part_elem = "core_part.position.z"
353                        elem_type = c_double
354                    elif temp_string == "velocity.x" or temp_string == "velocity[0]":
355                        part_elem = "core_part.velocity[0]"
356                        elem_type = c_double
357                    elif temp_string == "velocity.y" or temp_string == "velocity[1]":
358                        part_elem = "core_part.velocity[1]"
359                        elem_type = c_double
360                    elif temp_string == "velocity.z" or temp_string == "velocity[2]":
361                        part_elem = "core_part.velocity[2]"
362                        elem_type = c_double
363                elif part_elem.startswith("neighbour_part"):
364                    pass
365                else:
366                    elem_type = part_type.particle_type[part_elem]['type']
367                h5_type = HDF5_IO.type_map.get(elem_type, None)
368                if h5_type is None:
369                    assert False
370                elem_type = FDPS._type_map[elem_type]
371                code = code + self.indent() + "{0}* {1}_temp_array = ({0}*) malloc(sizeof({0}) * num_parts);\n".format(elem_type, key)
372                code = code + self.indent() + "H5Dread({0}_read_var, {1}, memspace, filespace, H5P_DEFAULT, {0}_temp_array);\n".format(key, h5_type)
373                code = code + self.indent() + "for( int i = 0; i < num_parts; i++){\n"
374                self.increment_indent()
375                code = code + self.indent() + "particle_system[i].{0} = {1}_temp_array[i];\n".format(part_elem, key)
376                self.decrement_indent()
377                code = code + self.indent() + "}\n"
378                code = code + self.indent() + "free({0}_temp_array);\n".format(key)
379                code = code + self.indent() + "H5Dclose({0}_read_var);\n".format(key)
380
381            # All data read in
382            code = code + "\n" + self.indent() + "H5Fclose(file_id);\n"
383            self.decrement_indent()
384            code = code + self.indent() + "}\n\n"
385
386        if len(self._outputs) > 0:
387            # Create the hdf5 output function
388            code = code + "\n"
389            code = code + self.indent() + "void hdf5_output(PS::ParticleSystem<FullParticle>& particle_system, config_type& config, const char* filename){\n"
390            self.increment_indent()
391            # Create the HDF5 file
392            code = code + self.indent() + "hid_t file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);\n"
393            code = code + self.indent() + "if( file_id < 0 ){\n"
394            self.increment_indent()
395            code = code + self.indent() + "printf(\"Couldn't create HDF5 file\\n\");\n"
396            code = code + self.indent() + "exit(1);\n"
397            self.decrement_indent()
398            code = code + self.indent() + "}\n\n"
399            # Setup dimensions of outputs
400            code = code + self.indent() + "hsize_t dims[1];\n"
401            code = code + self.indent() + "dims[0] = config.space.nparts;\n"
402            code = code + self.indent() + "hid_t dim = H5Screate_simple(1, dims, NULL);\n"
403
404            # Create dataset and output the requested values
405            for key in self._outputs.keys():
406                # Find the type of the variable from the particle structure
407                part_elem = self._outputs[key]
408                elem_type = None
409                h5_type = None
410                if part_elem.startswith("core_part"):
411                    temp_string = part_elem.replace("core_part.", "")
412                    if temp_string == "position.x" or temp_string == "position[0]":
413                        part_elem = "core_part.position.x"
414                        elem_type = c_double
415                    elif temp_string == "position.y" or temp_string == "position[1]":
416                        part_elem = "core_part.position.y"
417                        elem_type = c_double
418                    elif temp_string == "position.z" or temp_string == "position[2]":
419                        part_elem = "core_part.position.z"
420                        elem_type = c_double
421                    elif temp_string == "velocity.x" or temp_string == "velocity[0]":
422                        part_elem = "core_part.velocity[0]"
423                        elem_type = c_double
424                    elif temp_string == "velocity.y" or temp_string == "velocity[1]":
425                        part_elem = "core_part.velocity[1]"
426                        elem_type = c_double
427                    elif temp_string == "velocity.z" or temp_string == "velocity[2]":
428                        part_elem = "core_part.velocity[2]"
429                        elem_type = c_double
430                elif part_elem.startswith("neighbour_part"):
431                    pass
432                else:
433                    elem_type = part_type.particle_type[part_elem]['type']
434                h5_type = HDF5_IO.type_map.get(elem_type, None)
435                if h5_type is None:
436                    assert False
437                elem_type = FDPS._type_map[elem_type]
438                code = code + self.indent() + "hid_t {0}_output_field = H5Dcreate2(file_id, \"{0}\", {1}, dim, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);\n".format(
439                        key, h5_type)
440                # TODO Check if we successfully made the dataset
441                code = code + self.indent() + "{0}* {1}_output_array = ({0} *) malloc(sizeof({0}) * config.space.nparts);\n".format(elem_type, key)
442                code = code + self.indent() + "for( int i = 0; i < config.space.nparts; i++){\n"
443                self.increment_indent()
444                code = code + self.indent() + "{0}_output_array[i] = particle_system[i].{1};\n".format(key, part_elem)
445                self.decrement_indent()
446                code = code + self.indent() + "}\n\n"
447                code = code + self.indent() + "H5Dwrite({0}_output_field, {1}, H5S_ALL, H5S_ALL, H5P_DEFAULT, {0}_output_array);\n".format(key, h5_type)
448                code = code + self.indent() + "free({0}_output_array);\n".format(key)
449                code = code + self.indent() + "H5Dclose({0}_output_field);\n".format(key)
450
451            # End of output function
452            code = code + "\n" + self.indent() + "H5Fclose(file_id);\n"
453            self.decrement_indent()
454            code = code + self.indent() + "}\n\n"
455        return code
456
457    def call_input_fdps(self, part_count, filename, current_indent=4):
458        '''
459        Returns the FDPS call required to use this IO module for input.
460
461        :returns: The function call used for this module.
462        :rtype: str
463        '''
464        return f"hdf5_input(particle_system, config, {filename});"
465
466    def call_output_fdps(self, part_count, filename):
467        '''
468        Returns the FDPS call required to use this IO module for output.
469
470        :returns: The code required to use this IO module for output.
471        :rtype: str
472        '''
473        return f"hdf5_output(particle_system, config, {filename});"
474
475    def get_includes_fdps(self):
476        '''
477        Returns the includes required to use this IO module for FDPS.
478
479        :returns: The includes for this IO module.
480        :rtype: List of str
481        '''
482        includes = []
483        includes.append("\"hdf5.h\"")
484        return includes
485
Full Screen

PyAbcEcho.py

Source: PyAbcEcho.py Github

copy
1#-******************************************************************************
2#
3# Copyright (c) 2012,
4#  Sony Pictures Imageworks Inc. and
5#  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6#
7# All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions are
11# met:
12# *       Redistributions of source code must retain the above copyright
13# notice, this list of conditions and the following disclaimer.
14# *       Redistributions in binary form must reproduce the above
15# copyright notice, this list of conditions and the following disclaimer
16# in the documentation and/or other materials provided with the
17# distribution.
18# *       Neither the name of Sony Pictures Imageworks, nor
19# Industrial Light & Magic, nor the names of their contributors may be used
20# to endorse or promote products derived from this software without specific
21# prior written permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35#-******************************************************************************
36
37import sys
38from alembic.AbcCoreAbstract import *
39from alembic.Abc import *
40from alembic.Util import *
41
42def visitSmallArraySamples( iProp, iIndent ):
43    indent = " " * iIndent
44
45    for i, s in enumerate( iProp.arraySamples ):
46        print "%ssample %d:" % ( indent, i )
47        if s is None:
48            print indent, "********** Got nothing **********"
49        else:
50            print indent,
51            for j in s:
52                print j,
53            print
54
55def visitScalarSamples( iProp, iIndent ):
56    indent = " " * iIndent
57
58    for i, s in enumerate( iProp.scalarSamples ):
59        print "%ssample %d:" % ( indent, i )
60        if s is None:
61            print indent, "********** Got nothing **********"
62        else:
63            print "%s%s" % ( indent, s )
64
65def visitArraySamples( iProp, iIndent ):
66    indent = " " * iIndent
67
68    for i, s in enumerate( iProp.samples ):
69        print "%ssample %d:" % ( indent, i )
70        if s is None:
71            print indent, "********** Got nothing **********"
72        else:
73            print indent,
74            for j in s:
75                print j,
76            print 
77
78def visitCompoundProperty( iProp, iIndent ):
79    indent = " " * iIndent
80    ptype = "CompoundProperty"
81    name = "name=%s" % iProp.getName()
82    interp = "schema=%s" % iProp.getMetaData().get( "schema" )
83
84    print "%s%s %s; %s" % ( indent, ptype, name, interp )
85
86    visitProperties( iProp, iIndent+2 )
87
88def visitSimpleProperty( iProp, iIndent ):
89    indent = " " * iIndent
90    ptype = "ScalarProperty" if iProp.isScalar() else "ArrayProperty"
91    name = "name=%s" % iProp.getName()
92    md = "interpretation=%s" % iProp.getMetaData().get( "interpretation" )
93    dtype = "datatype=%s" % iProp.getDataType()
94    numsamps = "numsamps=%s" %iProp.getNumSamples()
95
96    print "%s%s %s; %s; %s; %s" % ( indent, ptype, name, md, dtype, numsamps )
97
98    if iProp.isScalar():
99        if iProp.getDataType().getExtent() == 1:
100            visitScalarSamples( iProp, iIndent+2 )
101        elif len( iProp.getMetaData().get( "interpretation" ) ) > 0:
102            visitScalarSamples( iProp, iIndent+2 )
103        else:
104            visitSmallArraySamples( iProp, iIndent+2 )
105    else:
106        visitArraySamples( iProp, iIndent+2 )
107
108
109def visitProperties( iParent, iIndent ):
110    for header in iParent.propertyheaders:
111        prop = iParent.getProperty( header.getName() )
112
113        if header.isCompound():
114            visitCompoundProperty( prop, iIndent+2 )
115        elif header.isScalar() or header.isArray():
116            visitSimpleProperty( prop, iIndent+2 )
117
118def visitObject( iObj, iIndent = 0 ):
119
120    path = iObj;
121    indent = " " * iIndent
122    ptype = "Object"
123    name = "name=%s" % path
124
125    if path != "/":
126        iIndent += 2
127        print "%s%s %s" % ( indent, ptype, name )
128
129    visitProperties( iObj.getProperties(), iIndent )
130
131    for child in iObj.children:
132        visitObject( child, iIndent )
133
134def visitArchive( iArg ):
135    iArchive = IArchive( iArg )
136
137    print "AbcEcho for %s" % GetLibraryVersion()
138
139    info = GetArchiveInfo ( iArchive )
140
141    if len( info['appName'] ) > 0:
142        print "  file written by : %s" % info['appName']
143        print "  using Alembic : %s" % info['libraryVersionString']
144        print "  written on : %s" % info['whenWritten']
145        print "  user description by: %s" % info['userDescription']
146        print
147    else:
148        print iArg
149        print "  (file doesn't have any ArchiveInfo)"
150        print
151
152    visitObject( iArchive.getTop() )
153
154
155map( visitArchive, [ a for a in sys.argv[1:] ] )
156
Full Screen

pprint.py

Source: pprint.py Github

copy
1#  Author:      Fred L. Drake, Jr.
2#               [email protected]
3#
4#  This is a simple little module I wrote to make life easier.  I didn't
5#  see anything quite like it in the library, though I may have overlooked
6#  something.  I wrote this when I was trying to read some heavily nested
7#  tuples with fairly non-descriptive content.  This is modeled very much
8#  after Lisp/Scheme - style pretty-printing of lists.  If you find it
9#  useful, thank small children who sleep at night.
10
11"""Support to pretty-print lists, tuples, & dictionaries recursively.
12
13Very simple, but useful, especially in debugging data structures.
14
15Classes
16-------
17
18PrettyPrinter()
19    Handle pretty-printing operations onto a stream using a configured
20    set of formatting parameters.
21
22Functions
23---------
24
25pformat()
26    Format a Python object into a pretty-printed representation.
27
28pprint()
29    Pretty-print a Python object to a stream [default is sys.stdout].
30
31saferepr()
32    Generate a 'standard' repr()-like value, but protect against recursive
33    data structures.
34
35"""
36
37import sys as _sys
38
39from cStringIO import StringIO as _StringIO
40
41__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
42           "PrettyPrinter"]
43
44# cache these for faster access:
45_commajoin = ", ".join
46_id = id
47_len = len
48_type = type
49
50
51def pprint(object, stream=None, indent=1, width=80, depth=None):
52    """Pretty-print a Python object to a stream [default is sys.stdout]."""
53    printer = PrettyPrinter(
54        stream=stream, indent=indent, width=width, depth=depth)
55    printer.pprint(object)
56
57def pformat(object, indent=1, width=80, depth=None):
58    """Format a Python object into a pretty-printed representation."""
59    return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)
60
61def saferepr(object):
62    """Version of repr() which can handle recursive data structures."""
63    return _safe_repr(object, {}, None, 0)[0]
64
65def isreadable(object):
66    """Determine if saferepr(object) is readable by eval()."""
67    return _safe_repr(object, {}, None, 0)[1]
68
69def isrecursive(object):
70    """Determine if object requires a recursive representation."""
71    return _safe_repr(object, {}, None, 0)[2]
72
73class PrettyPrinter:
74    def __init__(self, indent=1, width=80, depth=None, stream=None):
75        """Handle pretty printing operations onto a stream using a set of
76        configured parameters.
77
78        indent
79            Number of spaces to indent for each level of nesting.
80
81        width
82            Attempted maximum number of columns in the output.
83
84        depth
85            The maximum depth to print out nested structures.
86
87        stream
88            The desired output stream.  If omitted (or false), the standard
89            output stream available at construction will be used.
90
91        """
92        indent = int(indent)
93        width = int(width)
94        assert indent >= 0, "indent must be >= 0"
95        assert depth is None or depth > 0, "depth must be > 0"
96        assert width, "width must be != 0"
97        self._depth = depth
98        self._indent_per_level = indent
99        self._width = width
100        if stream is not None:
101            self._stream = stream
102        else:
103            self._stream = _sys.stdout
104
105    def pprint(self, object):
106        self._format(object, self._stream, 0, 0, {}, 0)
107        self._stream.write("\n")
108
109    def pformat(self, object):
110        sio = _StringIO()
111        self._format(object, sio, 0, 0, {}, 0)
112        return sio.getvalue()
113
114    def isrecursive(self, object):
115        return self.format(object, {}, 0, 0)[2]
116
117    def isreadable(self, object):
118        s, readable, recursive = self.format(object, {}, 0, 0)
119        return readable and not recursive
120
121    def _format(self, object, stream, indent, allowance, context, level):
122        level = level + 1
123        objid = _id(object)
124        if objid in context:
125            stream.write(_recursion(object))
126            self._recursive = True
127            self._readable = False
128            return
129        rep = self._repr(object, context, level - 1)
130        typ = _type(object)
131        sepLines = _len(rep) > (self._width - 1 - indent - allowance)
132        write = stream.write
133
134        if self._depth and level > self._depth:
135            write(rep)
136            return
137
138        r = getattr(typ, "__repr__", None)
139        if issubclass(typ, dict) and r is dict.__repr__:
140            write('{')
141            if self._indent_per_level > 1:
142                write((self._indent_per_level - 1) * ' ')
143            length = _len(object)
144            if length:
145                context[objid] = 1
146                indent = indent + self._indent_per_level
147                items  = object.items()
148                items.sort()
149                key, ent = items[0]
150                rep = self._repr(key, context, level)
151                write(rep)
152                write(': ')
153                self._format(ent, stream, indent + _len(rep) + 2,
154                              allowance + 1, context, level)
155                if length > 1:
156                    for key, ent in items[1:]:
157                        rep = self._repr(key, context, level)
158                        if sepLines:
159                            write(',\n%s%s: ' % (' '*indent, rep))
160                        else:
161                            write(', %s: ' % rep)
162                        self._format(ent, stream, indent + _len(rep) + 2,
163                                      allowance + 1, context, level)
164                indent = indent - self._indent_per_level
165                del context[objid]
166            write('}')
167            return
168
169        if ((issubclass(typ, list) and r is list.__repr__) or
170            (issubclass(typ, tuple) and r is tuple.__repr__) or
171            (issubclass(typ, set) and r is set.__repr__) or
172            (issubclass(typ, frozenset) and r is frozenset.__repr__)
173           ):
174            length = _len(object)
175            if issubclass(typ, list):
176                write('[')
177                endchar = ']'
178            elif issubclass(typ, set):
179                if not length:
180                    write('set()')
181                    return
182                write('set([')
183                endchar = '])'
184                object = sorted(object)
185                indent += 4
186            elif issubclass(typ, frozenset):
187                if not length:
188                    write('frozenset()')
189                    return
190                write('frozenset([')
191                endchar = '])'
192                object = sorted(object)
193                indent += 10
194            else:
195                write('(')
196                endchar = ')'
197            if self._indent_per_level > 1 and sepLines:
198                write((self._indent_per_level - 1) * ' ')
199            if length:
200                context[objid] = 1
201                indent = indent + self._indent_per_level
202                self._format(object[0], stream, indent, allowance + 1,
203                             context, level)
204                if length > 1:
205                    for ent in object[1:]:
206                        if sepLines:
207                            write(',\n' + ' '*indent)
208                        else:
209                            write(', ')
210                        self._format(ent, stream, indent,
211                                      allowance + 1, context, level)
212                indent = indent - self._indent_per_level
213                del context[objid]
214            if issubclass(typ, tuple) and length == 1:
215                write(',')
216            write(endchar)
217            return
218
219        write(rep)
220
221    def _repr(self, object, context, level):
222        repr, readable, recursive = self.format(object, context.copy(),
223                                                self._depth, level)
224        if not readable:
225            self._readable = False
226        if recursive:
227            self._recursive = True
228        return repr
229
230    def format(self, object, context, maxlevels, level):
231        """Format object for a specific context, returning a string
232        and flags indicating whether the representation is 'readable'
233        and whether the object represents a recursive construct.
234        """
235        return _safe_repr(object, context, maxlevels, level)
236
237
238# Return triple (repr_string, isreadable, isrecursive).
239
240def _safe_repr(object, context, maxlevels, level):
241    typ = _type(object)
242    if typ is str:
243        if 'locale' not in _sys.modules:
244            return repr(object), True, False
245        if "'" in object and '"' not in object:
246            closure = '"'
247            quotes = {'"': '\\"'}
248        else:
249            closure = "'"
250            quotes = {"'": "\\'"}
251        qget = quotes.get
252        sio = _StringIO()
253        write = sio.write
254        for char in object:
255            if char.isalpha():
256                write(char)
257            else:
258                write(qget(char, repr(char)[1:-1]))
259        return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
260
261    r = getattr(typ, "__repr__", None)
262    if issubclass(typ, dict) and r is dict.__repr__:
263        if not object:
264            return "{}", True, False
265        objid = _id(object)
266        if maxlevels and level >= maxlevels:
267            return "{...}", False, objid in context
268        if objid in context:
269            return _recursion(object), False, True
270        context[objid] = 1
271        readable = True
272        recursive = False
273        components = []
274        append = components.append
275        level += 1
276        saferepr = _safe_repr
277        for k, v in sorted(object.items()):
278            krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
279            vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
280            append("%s: %s" % (krepr, vrepr))
281            readable = readable and kreadable and vreadable
282            if krecur or vrecur:
283                recursive = True
284        del context[objid]
285        return "{%s}" % _commajoin(components), readable, recursive
286
287    if (issubclass(typ, list) and r is list.__repr__) or \
288       (issubclass(typ, tuple) and r is tuple.__repr__):
289        if issubclass(typ, list):
290            if not object:
291                return "[]", True, False
292            format = "[%s]"
293        elif _len(object) == 1:
294            format = "(%s,)"
295        else:
296            if not object:
297                return "()", True, False
298            format = "(%s)"
299        objid = _id(object)
300        if maxlevels and level >= maxlevels:
301            return format % "...", False, objid in context
302        if objid in context:
303            return _recursion(object), False, True
304        context[objid] = 1
305        readable = True
306        recursive = False
307        components = []
308        append = components.append
309        level += 1
310        for o in object:
311            orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level)
312            append(orepr)
313            if not oreadable:
314                readable = False
315            if orecur:
316                recursive = True
317        del context[objid]
318        return format % _commajoin(components), readable, recursive
319
320    rep = repr(object)
321    return rep, (rep and not rep.startswith('<')), False
322
323
324def _recursion(object):
325    return ("<Recursion on %s with id=%s>"
326            % (_type(object).__name__, _id(object)))
327
328
329def _perfcheck(object=None):
330    import time
331    if object is None:
332        object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000
333    p = PrettyPrinter()
334    t1 = time.time()
335    _safe_repr(object, {}, None, 0)
336    t2 = time.time()
337    p.pformat(object)
338    t3 = time.time()
339    print "_safe_repr:", t2 - t1
340    print "pformat:", t3 - t2
341
342if __name__ == "__main__":
343    _perfcheck()
344
Full Screen

plugin.js

Source: plugin.js Github

copy
1/*
2Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3For licensing, see LICENSE.html or http://ckeditor.com/license
4*/
5
6/**
7 * @file Increse and decrease indent commands.
8 */
9
10(function()
11{
12	var listNodeNames = { ol : 1, ul : 1 };
13
14	function setState( editor, state )
15	{
16		editor.getCommand( this.name ).setState( state );
17	}
18
19	function onSelectionChange( evt )
20	{
21		var elements = evt.data.path.elements,
22			listNode, listItem,
23			editor = evt.editor;
24
25		for ( var i = 0 ; i < elements.length ; i++ )
26		{
27			if ( elements[i].getName() == 'li' )
28			{
29				listItem = elements[i];
30				continue;
31			}
32			if ( listNodeNames[ elements[i].getName() ] )
33			{
34				listNode = elements[i];
35				break;
36			}
37		}
38
39		if ( listNode )
40		{
41			if ( this.name == 'outdent' )
42				return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );
43			else
44			{
45				while ( listItem && ( listItem = listItem.getPrevious( CKEDITOR.dom.walker.whitespaces( true ) ) ) )
46				{
47					if ( listItem.getName && listItem.getName() == 'li' )
48						return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );
49				}
50				return setState.call( this, editor, CKEDITOR.TRISTATE_DISABLED );
51			}
52		}
53
54		if ( !this.useIndentClasses && this.name == 'indent' )
55			return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );
56
57		var path = evt.data.path,
58			firstBlock = path.block || path.blockLimit;
59		if ( !firstBlock )
60			return setState.call( this, editor, CKEDITOR.TRISTATE_DISABLED );
61
62		if ( this.useIndentClasses )
63		{
64			var indentClass = firstBlock.$.className.match( this.classNameRegex ),
65				indentStep = 0;
66			if ( indentClass )
67			{
68				indentClass = indentClass[1];
69				indentStep = this.indentClassMap[ indentClass ];
70			}
71			if ( ( this.name == 'outdent' && !indentStep ) ||
72					( this.name == 'indent' && indentStep == editor.config.indentClasses.length ) )
73				return setState.call( this, editor, CKEDITOR.TRISTATE_DISABLED );
74			return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );
75		}
76		else
77		{
78			var indent = parseInt( firstBlock.getStyle( this.indentCssProperty ), 10 );
79			if ( isNaN( indent ) )
80				indent = 0;
81			if ( indent <= 0 )
82				return setState.call( this, editor, CKEDITOR.TRISTATE_DISABLED );
83			return setState.call( this, editor, CKEDITOR.TRISTATE_OFF );
84		}
85	}
86
87	function indentList( editor, range, listNode )
88	{
89		// Our starting and ending points of the range might be inside some blocks under a list item...
90		// So before playing with the iterator, we need to expand the block to include the list items.
91		var startContainer = range.startContainer,
92			endContainer = range.endContainer;
93		while ( startContainer && !startContainer.getParent().equals( listNode ) )
94			startContainer = startContainer.getParent();
95		while ( endContainer && !endContainer.getParent().equals( listNode ) )
96			endContainer = endContainer.getParent();
97
98		if ( !startContainer || !endContainer )
99			return;
100
101		// Now we can iterate over the individual items on the same tree depth.
102		var block = startContainer,
103			itemsToMove = [],
104			stopFlag = false;
105		while ( !stopFlag )
106		{
107			if ( block.equals( endContainer ) )
108				stopFlag = true;
109			itemsToMove.push( block );
110			block = block.getNext();
111		}
112		if ( itemsToMove.length < 1 )
113			return;
114
115		// Do indent or outdent operations on the array model of the list, not the
116		// list's DOM tree itself. The array model demands that it knows as much as
117		// possible about the surrounding lists, we need to feed it the further
118		// ancestor node that is still a list.
119		var listParents = listNode.getParents( true );
120		for ( var i = 0 ; i < listParents.length ; i++ )
121		{
122			if ( listParents[i].getName && listNodeNames[ listParents[i].getName() ] )
123			{
124				listNode = listParents[i];
125				break;
126			}
127		}
128		var indentOffset = this.name == 'indent' ? 1 : -1,
129			startItem = itemsToMove[0],
130			lastItem = itemsToMove[ itemsToMove.length - 1 ],
131			database = {};
132
133		// Convert the list DOM tree into a one dimensional array.
134		var listArray = CKEDITOR.plugins.list.listToArray( listNode, database );
135
136		// Apply indenting or outdenting on the array.
137		var baseIndent = listArray[ lastItem.getCustomData( 'listarray_index' ) ].indent;
138		for ( i = startItem.getCustomData( 'listarray_index' ) ; i <= lastItem.getCustomData( 'listarray_index' ) ; i++ )
139			listArray[i].indent += indentOffset;
140		for ( i = lastItem.getCustomData( 'listarray_index' ) + 1 ;
141				i < listArray.length && listArray[i].indent > baseIndent ; i++ )
142			listArray[i].indent += indentOffset;
143
144		// Convert the array back to a DOM forest (yes we might have a few subtrees now).
145		// And replace the old list with the new forest.
146		var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode, 0 );
147
148		// Avoid nested <li> after outdent even they're visually same,
149		// recording them for later refactoring.(#3982)
150		if ( this.name == 'outdent' )
151		{
152			var parentLiElement;
153			if ( ( parentLiElement = listNode.getParent() ) && parentLiElement.is( 'li' ) )
154			{
155				var children = newList.listNode.getChildren(),
156					pendingLis = [],
157					count = children.count(),
158					child;
159
160				for ( i = count - 1 ; i >= 0 ; i-- )
161				{
162					if( ( child = children.getItem( i ) ) && child.is && child.is( 'li' )  )
163						pendingLis.push( child );
164				}
165			}
166		}
167
168		if ( newList )
169			newList.listNode.replace( listNode );
170
171		// Move the nested <li> to be appeared after the parent.
172		if ( pendingLis && pendingLis.length )
173		{
174			for (  i = 0; i < pendingLis.length ; i++ )
175			{
176				var li = pendingLis[ i ],
177					followingList = li;
178
179				// Nest preceding <ul>/<ol> inside current <li> if any.
180				while( ( followingList = followingList.getNext() ) &&
181					   followingList.is &&
182					   followingList.getName() in listNodeNames )
183				{
184					li.append( followingList );
185				}
186
187				li.insertAfter( parentLiElement );
188			}
189		}
190
191		// Clean up the markers.
192		CKEDITOR.dom.element.clearAllMarkers( database );
193	}
194
195	function indentBlock( editor, range )
196	{
197		var iterator = range.createIterator(),
198			enterMode = editor.config.enterMode;
199		iterator.enforceRealBlocks = true;
200		iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
201		var block;
202		while ( ( block = iterator.getNextParagraph() ) )
203		{
204
205			if ( this.useIndentClasses )
206			{
207				// Transform current class name to indent step index.
208				var indentClass = block.$.className.match( this.classNameRegex ),
209					indentStep = 0;
210				if ( indentClass )
211				{
212					indentClass = indentClass[1];
213					indentStep = this.indentClassMap[ indentClass ];
214				}
215
216				// Operate on indent step index, transform indent step index back to class
217				// name.
218				if ( this.name == 'outdent' )
219					indentStep--;
220				else
221					indentStep++;
222				indentStep = Math.min( indentStep, editor.config.indentClasses.length );
223				indentStep = Math.max( indentStep, 0 );
224				var className = CKEDITOR.tools.ltrim( block.$.className.replace( this.classNameRegex, '' ) );
225				if ( indentStep < 1 )
226					block.$.className = className;
227				else
228					block.addClass( editor.config.indentClasses[ indentStep - 1 ] );
229			}
230			else
231			{
232				var currentOffset = parseInt( block.getStyle( this.indentCssProperty ), 10 );
233				if ( isNaN( currentOffset ) )
234					currentOffset = 0;
235				currentOffset += ( this.name == 'indent' ? 1 : -1 ) * editor.config.indentOffset;
236				currentOffset = Math.max( currentOffset, 0 );
237				currentOffset = Math.ceil( currentOffset / editor.config.indentOffset ) * editor.config.indentOffset;
238				block.setStyle( this.indentCssProperty, currentOffset ? currentOffset + editor.config.indentUnit : '' );
239				if ( block.getAttribute( 'style' ) === '' )
240					block.removeAttribute( 'style' );
241			}
242		}
243	}
244
245	function indentCommand( editor, name )
246	{
247		this.name = name;
248		this.useIndentClasses = editor.config.indentClasses && editor.config.indentClasses.length > 0;
249		if ( this.useIndentClasses )
250		{
251			this.classNameRegex = new RegExp( '(?:^|\\s+)(' + editor.config.indentClasses.join( '|' ) + ')(?=$|\\s)' );
252			this.indentClassMap = {};
253			for ( var i = 0 ; i < editor.config.indentClasses.length ; i++ )
254				this.indentClassMap[ editor.config.indentClasses[i] ] = i + 1;
255		}
256		else
257			this.indentCssProperty = editor.config.contentsLangDirection == 'ltr' ? 'margin-left' : 'margin-right';
258	}
259
260	indentCommand.prototype = {
261		exec : function( editor )
262		{
263			var selection = editor.getSelection(),
264				range = selection && selection.getRanges()[0];
265
266			if ( !selection || !range )
267				return;
268
269			var bookmarks = selection.createBookmarks( true ),
270				nearestListBlock = range.getCommonAncestor();
271
272			while ( nearestListBlock && !( nearestListBlock.type == CKEDITOR.NODE_ELEMENT &&
273				listNodeNames[ nearestListBlock.getName() ] ) )
274				nearestListBlock = nearestListBlock.getParent();
275
276			if ( nearestListBlock )
277				indentList.call( this, editor, range, nearestListBlock );
278			else
279				indentBlock.call( this, editor, range );
280
281			editor.focus();
282			editor.forceNextSelectionCheck();
283			selection.selectBookmarks( bookmarks );
284		}
285	};
286
287	CKEDITOR.plugins.add( 'indent',
288	{
289		init : function( editor )
290		{
291			// Register commands.
292			var indent = new indentCommand( editor, 'indent' ),
293				outdent = new indentCommand( editor, 'outdent' );
294			editor.addCommand( 'indent', indent );
295			editor.addCommand( 'outdent', outdent );
296
297			// Register the toolbar buttons.
298			editor.ui.addButton( 'Indent',
299				{
300					label : editor.lang.indent,
301					command : 'indent'
302				});
303			editor.ui.addButton( 'Outdent',
304				{
305					label : editor.lang.outdent,
306					command : 'outdent'
307				});
308
309			// Register the state changing handlers.
310			editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, indent ) );
311			editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, outdent ) );
312		},
313
314		requires : [ 'domiterator', 'list' ]
315	} );
316})();
317
318CKEDITOR.tools.extend( CKEDITOR.config,
319	{
320		indentOffset : 40,
321		indentUnit : 'px',
322		indentClasses : null
323	});
324
Full Screen

indent-legacy.js

Source: indent-legacy.js Github

copy
1/**
2 * @fileoverview This option sets a specific tab width for your code
3 *
4 * This rule has been ported and modified from nodeca.
5 * @author Vitaly Puzrin
6 * @author Gyandeep Singh
7 */
8
9"use strict";
10
11//------------------------------------------------------------------------------
12// Requirements
13//------------------------------------------------------------------------------
14
15const astUtils = require("../util/ast-utils");
16
17//------------------------------------------------------------------------------
18// Rule Definition
19//------------------------------------------------------------------------------
20
21/* istanbul ignore next: this rule has known coverage issues, but it's deprecated and shouldn't be updated in the future anyway. */
22module.exports = {
23    meta: {
24        type: "layout",
25
26        docs: {
27            description: "enforce consistent indentation",
28            category: "Stylistic Issues",
29            recommended: false,
30            url: "https://eslint.org/docs/rules/indent-legacy"
31        },
32
33        deprecated: true,
34
35        replacedBy: ["indent"],
36
37        fixable: "whitespace",
38
39        schema: [
40            {
41                oneOf: [
42                    {
43                        enum: ["tab"]
44                    },
45                    {
46                        type: "integer",
47                        minimum: 0
48                    }
49                ]
50            },
51            {
52                type: "object",
53                properties: {
54                    SwitchCase: {
55                        type: "integer",
56                        minimum: 0
57                    },
58                    VariableDeclarator: {
59                        oneOf: [
60                            {
61                                type: "integer",
62                                minimum: 0
63                            },
64                            {
65                                type: "object",
66                                properties: {
67                                    var: {
68                                        type: "integer",
69                                        minimum: 0
70                                    },
71                                    let: {
72                                        type: "integer",
73                                        minimum: 0
74                                    },
75                                    const: {
76                                        type: "integer",
77                                        minimum: 0
78                                    }
79                                }
80                            }
81                        ]
82                    },
83                    outerIIFEBody: {
84                        type: "integer",
85                        minimum: 0
86                    },
87                    MemberExpression: {
88                        type: "integer",
89                        minimum: 0
90                    },
91                    FunctionDeclaration: {
92                        type: "object",
93                        properties: {
94                            parameters: {
95                                oneOf: [
96                                    {
97                                        type: "integer",
98                                        minimum: 0
99                                    },
100                                    {
101                                        enum: ["first"]
102                                    }
103                                ]
104                            },
105                            body: {
106                                type: "integer",
107                                minimum: 0
108                            }
109                        }
110                    },
111                    FunctionExpression: {
112                        type: "object",
113                        properties: {
114                            parameters: {
115                                oneOf: [
116                                    {
117                                        type: "integer",
118                                        minimum: 0
119                                    },
120                                    {
121                                        enum: ["first"]
122                                    }
123                                ]
124                            },
125                            body: {
126                                type: "integer",
127                                minimum: 0
128                            }
129                        }
130                    },
131                    CallExpression: {
132                        type: "object",
133                        properties: {
134                            parameters: {
135                                oneOf: [
136                                    {
137                                        type: "integer",
138                                        minimum: 0
139                                    },
140                                    {
141                                        enum: ["first"]
142                                    }
143                                ]
144                            }
145                        }
146                    },
147                    ArrayExpression: {
148                        oneOf: [
149                            {
150                                type: "integer",
151                                minimum: 0
152                            },
153                            {
154                                enum: ["first"]
155                            }
156                        ]
157                    },
158                    ObjectExpression: {
159                        oneOf: [
160                            {
161                                type: "integer",
162                                minimum: 0
163                            },
164                            {
165                                enum: ["first"]
166                            }
167                        ]
168                    }
169                },
170                additionalProperties: false
171            }
172        ],
173        messages: {
174            expected: "Expected indentation of {{expected}} but found {{actual}}."
175        }
176    },
177
178    create(context) {
179        const DEFAULT_VARIABLE_INDENT = 1;
180        const DEFAULT_PARAMETER_INDENT = null; // For backwards compatibility, don't check parameter indentation unless specified in the config
181        const DEFAULT_FUNCTION_BODY_INDENT = 1;
182
183        let indentType = "space";
184        let indentSize = 4;
185        const options = {
186            SwitchCase: 0,
187            VariableDeclarator: {
188                var: DEFAULT_VARIABLE_INDENT,
189                let: DEFAULT_VARIABLE_INDENT,
190                const: DEFAULT_VARIABLE_INDENT
191            },
192            outerIIFEBody: null,
193            FunctionDeclaration: {
194                parameters: DEFAULT_PARAMETER_INDENT,
195