pardoc 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,9 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: pardoc
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Yet another docstring parser for python
5
5
  License: MIT
6
+ License-File: LICENSE
6
7
  Author: pwwang
7
8
  Author-email: pwwang@pwwang.com
8
9
  Requires-Python: >=3.9,<4.0
@@ -13,6 +14,8 @@ Classifier: Programming Language :: Python :: 3.10
13
14
  Classifier: Programming Language :: Python :: 3.11
14
15
  Classifier: Programming Language :: Python :: 3.12
15
16
  Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Programming Language :: Python :: 3.15
16
19
  Requires-Dist: diot (>=0.3,<0.4)
17
20
  Requires-Dist: lark (>=1.2,<2.0)
18
21
  Project-URL: Homepage, https://github.com/pwwang/pardoc
@@ -155,9 +158,9 @@ ParsedSection(title=Raises)
155
158
 
156
159
  ```python
157
160
  from pardoc import google_parser, numpy_parser
158
- parsed = google_parser(docstring)
161
+ parsed = google_parser.parse(docstring)
159
162
  # or
160
- parsed = numpy_parser(docstring)
163
+ parsed = numpy_parser.parse(docstring)
161
164
  ```
162
165
 
163
166
  ### Parsing an unknown style docstring
@@ -134,9 +134,9 @@ ParsedSection(title=Raises)
134
134
 
135
135
  ```python
136
136
  from pardoc import google_parser, numpy_parser
137
- parsed = google_parser(docstring)
137
+ parsed = google_parser.parse(docstring)
138
138
  # or
139
- parsed = numpy_parser(docstring)
139
+ parsed = numpy_parser.parse(docstring)
140
140
  ```
141
141
 
142
142
  ### Parsing an unknown style docstring
@@ -5,7 +5,7 @@ from .liquidpy import liquidpy_parser
5
5
  from .numpy import numpy_parser
6
6
  from .parsed import pretty
7
7
 
8
- __version__ = "0.2.0"
8
+ __version__ = "0.3.0"
9
9
 
10
10
 
11
11
  def auto_parser(docstring):
@@ -1,4 +1,6 @@
1
1
  """Abstract parsing class"""
2
+ from collections import OrderedDict
3
+ import copy
2
4
  import hashlib
3
5
  from lark import Lark, v_args, Transformer as LarkTransformer
4
6
  from lark.indenter import Indenter as LarkIndenter
@@ -151,9 +153,23 @@ class Parser:
151
153
  "ParsedSection": "_format_section",
152
154
  }
153
155
  POSTLEX = Indenter
156
+ CACHE_MAXSIZE = 128
154
157
 
155
158
  def __init__(self):
156
- self._cached = {}
159
+ self._cached = OrderedDict()
160
+ self._lark = None
161
+
162
+ def _get_lark(self):
163
+ """The compiled Lark parser, built once per parser instance.
164
+
165
+ Building it costs ~8.8 ms, against ~0.05 ms to parse a typical
166
+ docstring, so rebuilding it on every call dominated parse time.
167
+ """
168
+ if self._lark is None:
169
+ self._lark = Lark(
170
+ self.GRAMMER, parser="lalr", postlex=self.POSTLEX()
171
+ )
172
+ return self._lark
157
173
 
158
174
  def _format_para(
159
175
  self, elem, indent, leading_empty_line=True, indent_base=INDENT_BASE
@@ -419,9 +435,7 @@ class Parser:
419
435
  sections = Parsed()
420
436
  else:
421
437
  if self.POSTLEX:
422
- parser = Lark(
423
- self.GRAMMER, parser="lalr", postlex=self.POSTLEX()
424
- )
438
+ parser = self._get_lark()
425
439
  else: # pragma: no cover
426
440
  parser = Lark(self.GRAMMER, parser="lalr")
427
441
  tree = parser.parse(preprocessed)
@@ -451,12 +465,16 @@ class Parser:
451
465
 
452
466
  cache_key = hashlib.sha256(str(preprocessed).encode()).hexdigest()
453
467
  if cache_key in self._cached:
454
- return self._cached[cache_key]
468
+ self._cached.move_to_end(cache_key)
469
+ return copy.deepcopy(self._cached[cache_key])
455
470
 
456
471
  parsed = self._parse(preprocessed)
457
- self._cached[cache_key] = parsed
472
+ if self.CACHE_MAXSIZE > 0:
473
+ while len(self._cached) >= self.CACHE_MAXSIZE:
474
+ self._cached.popitem(last=False)
475
+ self._cached[cache_key] = parsed
458
476
 
459
- return parsed
477
+ return copy.deepcopy(parsed)
460
478
 
461
479
  def _format(self, parsed, indent="", indent_base=INDENT_BASE):
462
480
  """Format the parsed object
@@ -513,10 +531,20 @@ class Parser:
513
531
 
514
532
  Args:
515
533
  text_or_parsed (str|Parsed): The parsed object or the docstring
534
+ to (str): Target format, either 'text' or 'markdown'
516
535
 
517
536
  Return:
518
537
  str: The formatted docstring
538
+
539
+ Raises:
540
+ ValueError: If `to` is not 'text' or 'markdown'
519
541
  """
542
+ if to not in ("text", "markdown"):
543
+ raise ValueError(
544
+ f"Unsupported format target: {to!r}. "
545
+ "Expected 'text' or 'markdown'."
546
+ )
547
+
520
548
  if isinstance(text_or_parsed, str):
521
549
  parsed = self.parse(text_or_parsed)
522
550
  else:
@@ -18,7 +18,7 @@ class GoogleTransformer(Transformer):
18
18
  def ITEM(self, line):
19
19
  """Parse the item name, type and description"""
20
20
  match = re.match(
21
- r"^([A-Za-z_\*][\w_\*]*)(?:\s*\(([^\)]+)\))?\s*:\s*(.+)", str(line)
21
+ r"^([A-Za-z_\*][\w_\.\*]*)(?:\s*\(([^\)]+)\))?\s*:\s*(.+)", str(line)
22
22
  )
23
23
 
24
24
  parsed = ParsedItem(match.group(1), match.group(2), match.group(3), [])
@@ -64,7 +64,7 @@ class GoogleParser(Parser):
64
64
 
65
65
  def _preprocess(self, text):
66
66
  lines = text.splitlines()
67
- if not lines:
67
+ if not text.strip():
68
68
  return "\n"
69
69
  first_line = lines.pop(0)
70
70
  lines = textwrap.dedent("\n".join(lines)).splitlines()
@@ -1,8 +1,8 @@
1
- """Google style docstring parser"""
1
+ """Liquidpy style docstring parser"""
2
2
 
3
3
  import re
4
4
  import textwrap
5
- from lark import Lark, v_args
5
+ from lark import v_args
6
6
  from .default import SUMMARY, INDENT_BASE, Parser, Transformer
7
7
  from .parsed import (
8
8
  ParsedItem,
@@ -20,9 +20,9 @@ class LiquidpyTransformer(Transformer):
20
20
  def ITEM(self, line):
21
21
  """Parse the item name, type and description"""
22
22
  match = re.match(
23
- r"^([A-Za-z_\*][\w_\*]*)(?:\s*\(([^\)]+)\))?\s*:\s*(.+)", str(line)
23
+ r"^([A-Za-z_\*][\w_\.\*]*)(?:\s*\(([^\)]+)\))?\s*:\s*(.+)", str(line)
24
24
  ) or re.match(
25
- r"^\(\s*([A-Za-z_\*][\w_\*]*)\s*\)(\s*):\s*(.+)", str(line)
25
+ r"^\(\s*([A-Za-z_\*][\w_\.\*]*)\s*\)(\s*):\s*(.+)", str(line)
26
26
  )
27
27
  item_name = match.group(1)
28
28
  item_type = match.group(2)
@@ -79,7 +79,7 @@ class LiquidpyParser(Parser):
79
79
 
80
80
  def _preprocess(self, text):
81
81
  lines = text.splitlines()
82
- if not lines:
82
+ if not text.strip():
83
83
  return "\n", False
84
84
  first_line = lines.pop(0)
85
85
  lines = textwrap.dedent("\n".join(lines)).splitlines()
@@ -87,6 +87,8 @@ class LiquidpyParser(Parser):
87
87
  api = first_line == "@API"
88
88
  if not first_line or api:
89
89
  first_line = "" if not lines else lines.pop(0)
90
+ if not first_line.strip() and not lines:
91
+ return "\n", api
90
92
 
91
93
  preprocessed = ["@" + SUMMARY + ":", INDENT_BASE + first_line]
92
94
 
@@ -106,7 +108,7 @@ class LiquidpyParser(Parser):
106
108
  if preprocessed == "\n":
107
109
  sections = Parsed()
108
110
  else:
109
- parser = Lark(self.GRAMMER, parser="lalr", postlex=self.POSTLEX())
111
+ parser = self._get_lark()
110
112
  tree = parser.parse(preprocessed)
111
113
  sections = self.TRANSFORMER.transform(tree)
112
114
  if isinstance(sections, ParsedSection):
@@ -19,9 +19,10 @@ class NumpyTransformer(Transformer):
19
19
  def ITEM(self, line):
20
20
  """Parse the item name, type and description"""
21
21
  match = re.match(
22
- r"^(\*{1,2}[\w_]+|[A-Za-z_][\w_]*)\s*:\s*([A-Za-z_][\w ,\[\]]*)$",
22
+ r"^(\*{1,2}[\w_\.]+|[A-Za-z_][\w_\.]*)"
23
+ r"\s*:\s*([A-Za-z_][\w ,\[\]]*)$",
23
24
  str(line),
24
- ) or re.match(r"^([A-Za-z_][\w ,\[\]]*)(\s*)$", str(line))
25
+ ) or re.match(r"^([A-Za-z_][\w_\. ,\[\]]*)(\s*)$", str(line))
25
26
  item_name = match.group(1)
26
27
  item_type = match.group(2)
27
28
 
@@ -60,12 +61,9 @@ class NumpyParser(Parser):
60
61
  codeblock: _CODETAG LANG? _NL _INDENT? paragraph+ _DEDENT? _CODETAG _NL?
61
62
  !para_line: /.+/ _NL
62
63
 
63
- %import common.CNAME -> NAME
64
64
  %import common.WS_INLINE
65
65
  %import common.CR -> _CR
66
66
  %import common.LF -> _LF
67
- %import common.WS_INLINE
68
- %import common.NEWLINE -> _NEWLINE
69
67
  %declare _INDENT _DEDENT
70
68
  %ignore WS_INLINE
71
69
 
@@ -81,7 +79,7 @@ class NumpyParser(Parser):
81
79
 
82
80
  def _preprocess(self, text):
83
81
  lines = text.splitlines()
84
- if not lines:
82
+ if not text.strip():
85
83
  return "\n"
86
84
  first_line = lines.pop(0)
87
85
  lines = textwrap.dedent("\n".join(lines)).splitlines()
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
4
4
 
5
5
  [tool.poetry]
6
6
  name = "pardoc"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "Yet another docstring parser for python"
9
9
  authors = [ "pwwang <pwwang@pwwang.com>",]
10
10
  license = "MIT"
File without changes
File without changes