pyOpenVBA 5.1.2__tar.gz → 5.2.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.
Files changed (89) hide show
  1. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/PKG-INFO +6 -1
  2. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/README.md +5 -0
  3. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/pyproject.toml +1 -1
  4. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/__init__.py +1 -1
  5. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_index.py +6 -3
  6. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_rows.py +30 -3
  7. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_vba.py +46 -16
  8. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/database.py +209 -56
  9. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/.gitignore +0 -0
  10. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/LICENSE.md +0 -0
  11. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/architecture.md +0 -0
  12. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/ms-ovba-implementation-guide_v2.md +0 -0
  13. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/power_query.md +0 -0
  14. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/research/access_complex/README.md +0 -0
  15. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/research/access_designs/README.md +0 -0
  16. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/research/access_macros/README.md +0 -0
  17. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/research/access_write/README.md +0 -0
  18. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/research/pcode/README.md +0 -0
  19. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/docs/roadmap.md +0 -0
  20. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/__main__.py +0 -0
  21. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_deflate.py +0 -0
  22. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_host.py +0 -0
  23. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_oforms_pages.py +0 -0
  24. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_oforms_records.py +0 -0
  25. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_ppt_container.py +0 -0
  26. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/__init__.py +0 -0
  27. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/blank_database.accdb +0 -0
  28. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/blank_database_module.accdb +0 -0
  29. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/blank_document.docm +0 -0
  30. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/blank_excel_addin.xlam +0 -0
  31. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/blank_presentation.pptm +0 -0
  32. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/blank_workbook.xlsb +0 -0
  33. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/blank_workbook.xlsm +0 -0
  34. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/blank_workbook.xlsx +0 -0
  35. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/engine_skeleton.accdb +0 -0
  36. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/blank_files/engine_skeleton.mdb +0 -0
  37. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/form.blob +0 -0
  38. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/form.lvprop +0 -0
  39. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/form.propdata +0 -0
  40. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/form.prototypes +0 -0
  41. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/form.typeinfo +0 -0
  42. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/report.blob +0 -0
  43. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/report.lvprop +0 -0
  44. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/report.propdata +0 -0
  45. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/report.prototypes +0 -0
  46. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/_templates/designs/report.typeinfo +0 -0
  47. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/__init__.py +0 -0
  48. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_alloc.py +0 -0
  49. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_btree.py +0 -0
  50. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_collation.py +0 -0
  51. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_collation_general_legacy.py +0 -0
  52. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_compact.py +0 -0
  53. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_complex.py +0 -0
  54. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_datapage.py +0 -0
  55. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_ddl.py +0 -0
  56. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_designs.py +0 -0
  57. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_facade.py +0 -0
  58. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_format.py +0 -0
  59. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_lval.py +0 -0
  60. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_macros.py +0 -0
  61. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_pages.py +0 -0
  62. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_props.py +0 -0
  63. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_queries.py +0 -0
  64. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_schema.py +0 -0
  65. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_sql.py +0 -0
  66. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_storage.py +0 -0
  67. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_tdef.py +0 -0
  68. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access/_validate.py +0 -0
  69. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/access_read.py +0 -0
  70. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/cfb.py +0 -0
  71. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/excel.py +0 -0
  72. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/exceptions.py +0 -0
  73. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/forms.py +0 -0
  74. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerpoint.py +0 -0
  75. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/__init__.py +0 -0
  76. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_binary.py +0 -0
  77. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_files.py +0 -0
  78. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_mashup.py +0 -0
  79. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_metadata.py +0 -0
  80. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_opc.py +0 -0
  81. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_package.py +0 -0
  82. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_refresh.py +0 -0
  83. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_section.py +0 -0
  84. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/_sheets.py +0 -0
  85. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/powerquery/workbook.py +0 -0
  86. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/vba.py +0 -0
  87. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/vba_pcode.py +0 -0
  88. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/src/pyopenvba/word.py +0 -0
  89. {pyopenvba-5.1.2 → pyopenvba-5.2.0}/tests/fuzz_corpus/README.md +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pyOpenVBA
3
- Version: 5.1.2
3
+ Version: 5.2.0
4
4
  Summary: Read and write VBA macros and Excel Power Query in pure Python, no dependencies.
5
5
  Project-URL: Homepage, https://github.com/WilliamSmithEdward/pyOpenVBA
6
6
  Project-URL: Repository, https://github.com/WilliamSmithEdward/pyOpenVBA
@@ -385,10 +385,15 @@ with AccessDatabase("app.accdb") as db:
385
385
 
386
386
  report = db.add_report("Monthly")
387
387
  report.add_control("Label", "Banner", section="PageHeaderSection", caption="Header band")
388
+ db.rename_form("Draft", "Invoice")
388
389
  db.delete_form("Old")
389
390
  db.save()
390
391
  ```
391
392
 
393
+ Renaming and deleting reach the code behind a design as well as the
394
+ design itself. A form's module is bound to it by name, so `Form_Draft`
395
+ becomes `Form_Invoice` and a deleted form takes its module with it.
396
+
392
397
  [examples/access_form_demo.py](https://github.com/WilliamSmithEdward/pyOpenVBA/blob/main/examples/access_form_demo.py)
393
398
  builds a working order calculator this way: a form whose buttons call a
394
399
  standard module and keep their running total in a class module, laid out
@@ -349,10 +349,15 @@ with AccessDatabase("app.accdb") as db:
349
349
 
350
350
  report = db.add_report("Monthly")
351
351
  report.add_control("Label", "Banner", section="PageHeaderSection", caption="Header band")
352
+ db.rename_form("Draft", "Invoice")
352
353
  db.delete_form("Old")
353
354
  db.save()
354
355
  ```
355
356
 
357
+ Renaming and deleting reach the code behind a design as well as the
358
+ design itself. A form's module is bound to it by name, so `Form_Draft`
359
+ becomes `Form_Invoice` and a deleted form takes its module with it.
360
+
356
361
  [examples/access_form_demo.py](https://github.com/WilliamSmithEdward/pyOpenVBA/blob/main/examples/access_form_demo.py)
357
362
  builds a working order calculator this way: a form whose buttons call a
358
363
  standard module and keep their running total in a class module, laid out
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "pyOpenVBA"
7
- version = "5.1.2"
7
+ version = "5.2.0"
8
8
  description = "Read and write VBA macros and Excel Power Query in pure Python, no dependencies."
9
9
  readme = "README.md"
10
10
  license = { text = "MIT" }
@@ -291,4 +291,4 @@ def push_power_query(
291
291
  """
292
292
  return _push_queries(src_dir, workbook, out=out, encoding=encoding, remove_missing=remove_missing)
293
293
 
294
- __version__ = "5.1.2"
294
+ __version__ = "5.2.0"
@@ -58,7 +58,7 @@ from pyopenvba.access._pages import (
58
58
  PAGE_SIZE,
59
59
  PageStore,
60
60
  )
61
- from pyopenvba.access._rows import decode_datetime
61
+ from pyopenvba.access._rows import decode_datetime, scaled_decimal, scaled_int
62
62
  from pyopenvba.access._tdef import (
63
63
  TYPE_BIGINT,
64
64
  TYPE_BINARY,
@@ -463,7 +463,10 @@ def _encode_value(column: ColumnDef, value: object) -> bytes:
463
463
  if code == TYPE_NUMERIC:
464
464
  if not isinstance(value, (int, Decimal)):
465
465
  raise AccessError(f"column {column.name!r}: {value!r} is not a Decimal")
466
- scaled = int(Decimal(value).scaleb(column.scale))
466
+ # Through ``scaled_int``, so the key rounds a fractional value the
467
+ # way the row encoder does and carries a 29-digit one without the
468
+ # default context rounding it (GitHub issue #20).
469
+ scaled = scaled_int(Decimal(value), column.scale)
467
470
  magnitude = abs(scaled).to_bytes(16, "big")
468
471
  return b"\xff" + magnitude if scaled >= 0 else b"\x00" + _invert(magnitude)
469
472
  if code == TYPE_GUID:
@@ -510,5 +513,5 @@ def _decode_fixed(column: ColumnDef, raw: bytes) -> object:
510
513
  value = -Decimal(int.from_bytes(_invert(magnitude), "big"))
511
514
  else:
512
515
  raise AccessError(f"column {column.name!r}: Decimal key sign byte {sign:#04x}")
513
- return value.scaleb(-column.scale)
516
+ return scaled_decimal(value, -column.scale)
514
517
  raise AccessError(f"column {column.name!r}: no fixed key codec for {column.type_name}")
@@ -23,7 +23,7 @@ import datetime as _dt
23
23
  import struct
24
24
  import uuid
25
25
  from dataclasses import dataclass
26
- from decimal import ROUND_HALF_EVEN, Decimal
26
+ from decimal import ROUND_HALF_EVEN, Decimal, localcontext
27
27
 
28
28
  from pyopenvba.access_read import AccessError
29
29
  from pyopenvba.access._tdef import (
@@ -203,6 +203,33 @@ def encode_datetime(value: _dt.datetime) -> bytes:
203
203
  return struct.pack("<d", number)
204
204
 
205
205
 
206
+ #: A Decimal column is a 16-byte magnitude, so a value carries up to 39
207
+ #: digits, while Python's default arithmetic context rounds at 28. Every
208
+ #: Decimal conversion here and in the index key codec runs at this
209
+ #: precision instead: scaling ``2**96 - 1`` through the default context
210
+ #: returns it five too high, which puts a key out of step with the row it
211
+ #: points at (GitHub issue #20).
212
+ DECIMAL_PREC = 64
213
+
214
+
215
+ def scaled_decimal(value: Decimal | int, scale: int) -> Decimal:
216
+ """``value`` shifted by ``10 ** scale``, with no context rounding."""
217
+ with localcontext() as ctx:
218
+ ctx.prec = DECIMAL_PREC
219
+ return Decimal(value).scaleb(scale)
220
+
221
+
222
+ def scaled_int(value: Decimal, scale: int) -> int:
223
+ """``value`` shifted by ``10 ** scale`` and rounded to an integer.
224
+
225
+ Half-even, as Access rounds, and at a precision no Decimal a column
226
+ can hold will overflow.
227
+ """
228
+ with localcontext() as ctx:
229
+ ctx.prec = DECIMAL_PREC
230
+ return int(Decimal(value).scaleb(scale).to_integral_value())
231
+
232
+
206
233
  def decode_numeric(raw: bytes, scale: int) -> Decimal:
207
234
  if len(raw) != 17:
208
235
  raise AccessError(f"Decimal value is {len(raw)} bytes, not 17")
@@ -211,12 +238,12 @@ def decode_numeric(raw: bytes, scale: int) -> Decimal:
211
238
  magnitude = 0
212
239
  for word in words:
213
240
  magnitude = (magnitude << 32) | word
214
- value = Decimal(magnitude).scaleb(-scale)
241
+ value = scaled_decimal(magnitude, -scale)
215
242
  return -value if negative else value
216
243
 
217
244
 
218
245
  def encode_numeric(value: Decimal, scale: int) -> bytes:
219
- scaled = int((value.copy_abs() * (Decimal(10) ** scale)).to_integral_value())
246
+ scaled = scaled_int(value.copy_abs(), scale)
220
247
  if scaled >= 1 << 128:
221
248
  raise AccessError("Decimal value does not fit 16 bytes")
222
249
  words = [(scaled >> shift) & 0xFFFFFFFF for shift in (96, 64, 32, 0)]
@@ -368,6 +368,12 @@ def rename_attribute(text: str, old: str, new: str) -> str:
368
368
 
369
369
 
370
370
  # --- PROJECTwm and PROJECT --------------------------------------------------
371
+ #: How PROJECT names the module behind a form or report: the keyword, the
372
+ #: module's name, a slash, and a flag word Access owns. A design's module
373
+ #: is listed this way and never as ``Module=`` or ``Class=``.
374
+ DOC_CLASS = "DocClass"
375
+ #: The three keywords that open the module block.
376
+ MODULE_KEYWORDS = ("Module=", "Class=", DOC_CLASS + "=")
371
377
 
372
378
 
373
379
  def project_wm_entry(name: str, encoding: str) -> bytes:
@@ -395,34 +401,56 @@ def rename_project_wm(payload: bytes, old: str, new: str, encoding: str) -> byte
395
401
  def add_to_project(text: str, name: str, kind: str) -> str:
396
402
  """Access lists a standard module as ``Module=`` and a class as
397
403
  ``Class=``, both in the same block, and gives each a window rectangle
398
- under ``[Workspace]``."""
404
+ under ``[Workspace]``.
405
+
406
+ The block can be empty -- delete a project's last module and there is
407
+ no line to sit after -- and Access opens it right below the ``ID=``
408
+ line, which is where the first one goes.
409
+ """
399
410
  lines = text.split(CRLF)
400
- last = max(i for i, line in enumerate(lines) if line.startswith(("Module=", "Class=")))
401
- lines.insert(last + 1, ("Class=" if kind == "class" else "Module=") + name)
411
+ entry = ("Class=" if kind == "class" else "Module=") + name
412
+ listed = [i for i, line in enumerate(lines) if line.startswith(MODULE_KEYWORDS)]
413
+ if listed:
414
+ lines.insert(max(listed) + 1, entry)
415
+ else:
416
+ opener = next((i for i, line in enumerate(lines) if line.startswith("ID=")), -1)
417
+ lines.insert(opener + 1, entry)
402
418
  if any(line.strip() == "[Workspace]" for line in lines):
403
419
  lines.insert(len(lines) - 1, f"{name}=38, 38, 1786, 1030, ")
404
420
  return CRLF.join(lines)
405
421
 
406
422
 
407
423
  def remove_from_project(text: str, name: str) -> str:
424
+ """Every line naming a module: its ``Module=``, ``Class=`` or
425
+ ``DocClass=`` line, and its ``[Workspace]`` rectangle."""
408
426
  lines = [
409
427
  line
410
428
  for line in text.split(CRLF)
411
429
  if line not in (f"Module={name}", f"Class={name}")
430
+ and not line.startswith(f"{DOC_CLASS}={name}/")
412
431
  and not re.match(re.escape(name) + "=", line)
413
432
  ]
414
433
  return CRLF.join(lines)
415
434
 
416
435
 
417
436
  def rename_project(text: str, old: str, new: str) -> str:
418
- """The ``Module=``/``Class=`` line and the ``[Workspace]`` line. The
419
- stream's lines end CR LF, so the end anchor has to allow the CR."""
437
+ """The ``Module=``, ``Class=`` or ``DocClass=`` line and the
438
+ ``[Workspace]`` line. The stream's lines end CR LF, so the end anchor
439
+ has to allow the CR.
440
+
441
+ A design's module is listed only as ``DocClass=<name>/<flags>``, never
442
+ as ``Module=`` or ``Class=``. Renaming without reaching it leaves a
443
+ DocClass naming a module the project no longer has, which Access
444
+ reports as a corrupt project on the first VBE reference (GitHub issue
445
+ #21). The flag word after the slash is Access's, so the match stops
446
+ at it and it is carried through.
447
+ """
448
+ quoted = re.escape(old)
420
449
  tail = "(?=" + chr(92) + "r?$)"
421
450
  for keyword in ("Module", "Class"):
422
- text = re.sub(
423
- "(?m)^" + keyword + "=" + re.escape(old) + tail, keyword + "=" + new, text
424
- )
425
- return re.sub("(?m)^" + re.escape(old) + "=", new + "=", text)
451
+ text = re.sub("(?m)^" + keyword + "=" + quoted + tail, keyword + "=" + new, text)
452
+ text = re.sub("(?m)^" + DOC_CLASS + "=" + quoted + "(?=/)", DOC_CLASS + "=" + new, text)
453
+ return re.sub("(?m)^" + quoted + "=", new + "=", text)
426
454
 
427
455
 
428
456
  # --- the folder list ---------------------------------------------------------
@@ -476,22 +504,24 @@ def document_attributes(name: str, clsid: str) -> list[str]:
476
504
  def add_to_project_documents(text: str, name: str) -> str:
477
505
  """A `DocClass=` line, and a window rectangle under `[Workspace]`."""
478
506
  lines = text.split(CRLF)
479
- last = max(
480
- i
481
- for i, line in enumerate(lines)
482
- if line.startswith(("Module=", "Class=", "DocClass="))
483
- )
484
- lines.insert(last + 1, f"DocClass={name}{DOC_CLASS_SUFFIX}")
507
+ last = max(i for i, line in enumerate(lines) if line.startswith(MODULE_KEYWORDS))
508
+ lines.insert(last + 1, f"{DOC_CLASS}={name}{DOC_CLASS_SUFFIX}")
485
509
  if any(line.strip() == "[Workspace]" for line in lines):
486
510
  lines.insert(len(lines) - 1, f"{name}={DOC_WORKSPACE}")
487
511
  return CRLF.join(lines)
488
512
 
489
513
 
490
514
  def remove_from_project_documents(text: str, name: str) -> str:
515
+ """A document module's ``DocClass=`` line and its workspace rectangle.
516
+
517
+ Matched on the prefix, because the flag word after the slash and the
518
+ rectangle are Access's and a database it has edited need not carry the
519
+ ones written here.
520
+ """
491
521
  return CRLF.join(
492
522
  line
493
523
  for line in text.split(CRLF)
494
- if line != f"DocClass={name}{DOC_CLASS_SUFFIX}" and line != f"{name}={DOC_WORKSPACE}"
524
+ if not line.startswith(f"{DOC_CLASS}={name}/") and not line.startswith(f"{name}=")
495
525
  )
496
526
 
497
527
 
@@ -2641,6 +2641,43 @@ class AccessDatabase:
2641
2641
  footer sections."""
2642
2642
  return AccessForm(self, self._create_design("report", name, updated=updated))
2643
2643
 
2644
+ def _list_in_container(
2645
+ self, container: int, name: str, folder: str, when: dt.datetime | float
2646
+ ) -> None:
2647
+ """Name an object in its container's listing and claim its folder
2648
+ in the container's folder list, creating either stream when the
2649
+ container has none yet.
2650
+
2651
+ A container that has never held an object carries neither, so a
2652
+ module added to a project with none went unlisted, and Access
2653
+ showed nothing under `AllModules` for a module its VBE still
2654
+ listed (GitHub issue #21).
2655
+ """
2656
+ storage = self.table(STORAGE_TABLE)
2657
+ adders: tuple[tuple[str, Callable[[bytes], bytes]], ...] = (
2658
+ (DIR_DATA, lambda payload: add_to_dir_data(payload, name, folder)),
2659
+ ("PropData", lambda payload: add_to_folder_list(payload, folder)),
2660
+ )
2661
+ for stream, add in adders:
2662
+ found = next(
2663
+ (
2664
+ (rid, row.get("Lv"))
2665
+ for rid, row in storage.rows_with_ids()
2666
+ if _as_int(row["ParentId"]) == container and str(row["Name"]) == stream
2667
+ ),
2668
+ None,
2669
+ )
2670
+ if found is None:
2671
+ storage.insert_row(
2672
+ {"ParentId": container, "Name": stream, "Type": TYPE_VALUE,
2673
+ "Lv": add(bytes(4)), "DateCreate": when, "DateUpdate": when}
2674
+ )
2675
+ else:
2676
+ rid, payload = found
2677
+ storage.update_row(
2678
+ rid, {"Lv": add(payload if isinstance(payload, bytes) else bytes(4))}
2679
+ )
2680
+
2644
2681
  def _create_design(self, kind: str, name: str, *, updated: object | None) -> AccessDesign:
2645
2682
  """The design itself comes from a captured template -- an empty one
2646
2683
  as Access writes it -- with a GUID of its own patched in, since the
@@ -2686,29 +2723,7 @@ class AccessDatabase:
2686
2723
  values["Lv"] = payload
2687
2724
  storage.insert_row(values)
2688
2725
 
2689
- adders: tuple[tuple[str, Callable[[bytes], bytes]], ...] = (
2690
- (DIR_DATA, lambda payload: add_to_dir_data(payload, name, folder)),
2691
- ("PropData", lambda payload: add_to_folder_list(payload, folder)),
2692
- )
2693
- for stream, add in adders:
2694
- found = next(
2695
- (
2696
- (rid, row.get("Lv"))
2697
- for rid, row in storage.rows_with_ids()
2698
- if _as_int(row["ParentId"]) == container and str(row["Name"]) == stream
2699
- ),
2700
- None,
2701
- )
2702
- if found is None:
2703
- storage.insert_row(
2704
- {"ParentId": container, "Name": stream, "Type": TYPE_VALUE,
2705
- "Lv": add(bytes(4)), "DateCreate": when, "DateUpdate": when}
2706
- )
2707
- else:
2708
- rid, payload = found
2709
- storage.update_row(
2710
- rid, {"Lv": add(payload if isinstance(payload, bytes) else bytes(4))}
2711
- )
2726
+ self._list_in_container(container, name, folder, when)
2712
2727
 
2713
2728
  objects = self.table("MSysObjects")
2714
2729
  parent = next(e.id for e in self.catalog() if e.name == CATALOG_CONTAINERS[kind] and e.type == 3)
@@ -2988,6 +3003,99 @@ class AccessDatabase:
2988
3003
  return self.module(name)
2989
3004
 
2990
3005
 
3006
+ def _delete_module_streams(self, module: VBAModule) -> None:
3007
+ """Take a module out of the VBA project alone: its stream row, its
3008
+ dir block, its PROJECTwm entry, and whichever line of PROJECT
3009
+ names it.
3010
+
3011
+ The Modules container is deliberately untouched, because the code
3012
+ behind a form or report is listed in neither container: the
3013
+ project carries `Form_Calculator` while the Forms listing carries
3014
+ `Calculator`.
3015
+ """
3016
+ storage = self.table(STORAGE_TABLE)
3017
+ encoding = encoding_of(self._vba_dir()[1])
3018
+ _modules, project_id, streams_id = self._vba_storage_ids()
3019
+ for rid, row in list(storage.rows_with_ids()):
3020
+ value = row.get("Lv")
3021
+ row_name, parent = str(row["Name"]), _as_int(row["ParentId"])
3022
+ if parent == streams_id and row_name == module.stream_name:
3023
+ storage.delete_row(rid, retire_empty=False)
3024
+ continue
3025
+ if not isinstance(value, bytes) or not value:
3026
+ continue
3027
+ if row_name == "dir":
3028
+ storage.update_row(
3029
+ rid, {"Lv": compress(remove_from_dir(decompress(value), module.name))}
3030
+ )
3031
+ elif row_name == "_VBA_PROJECT":
3032
+ storage.update_row(rid, {"Lv": invalidate_cache(value)})
3033
+ elif row_name == "PROJECTwm" and parent == project_id:
3034
+ storage.update_row(rid, {"Lv": remove_from_project_wm(value, module.name, encoding)})
3035
+ elif row_name == "PROJECT" and parent == project_id:
3036
+ text = value.decode(encoding, errors="replace")
3037
+ fixed = remove_from_project(text, module.name)
3038
+ if fixed != text:
3039
+ storage.update_row(rid, {"Lv": encode_mbcs(fixed, encoding)})
3040
+ self._drop_srp()
3041
+
3042
+ def _design_module(self, kind: str, name: str) -> VBAModule | None:
3043
+ """The module behind a design, if Access ever opened a code window
3044
+ for it. A design that has none simply has no such module."""
3045
+ wanted = self.DESIGN_MODULE_PREFIX[kind] + name
3046
+ return next(
3047
+ (module for module in self.modules() if module.name.lower() == wanted.lower()),
3048
+ None,
3049
+ )
3050
+
3051
+ def rename_form(self, name: str, new_name: str) -> None:
3052
+ """Rename a form everywhere its name lives."""
3053
+ self._rename_design("form", name, new_name)
3054
+
3055
+ def rename_report(self, name: str, new_name: str) -> None:
3056
+ """Rename a report everywhere its name lives."""
3057
+ self._rename_design("report", name, new_name)
3058
+
3059
+ def _rename_design(self, kind: str, name: str, new_name: str) -> None:
3060
+ """A design's name lives in four places: its container's listing,
3061
+ its catalog row, its navigation-pane row, and the module behind
3062
+ it, which Access binds by name as `Form_<name>` or
3063
+ `Report_<name>`.
3064
+
3065
+ A design Access has never opened a code window for has no module,
3066
+ and then there are three. The module is the part with the sharp
3067
+ edge: `PROJECT` names it only as a `DocClass=` line, and leaving
3068
+ that naming a module the project no longer has makes Access report
3069
+ the whole project as corrupt on the first VBE reference (GitHub
3070
+ issue #21).
3071
+ """
3072
+ if not new_name or len(new_name) > 64:
3073
+ raise AccessError(f"a {kind} name is 1 to 64 characters")
3074
+ found = self._design(kind, name)
3075
+ if new_name.lower() != found.name.lower() and any(
3076
+ other.name.lower() == new_name.lower() for other in self._designs(kind)
3077
+ ):
3078
+ raise AccessError(f"a {kind} named {new_name!r} already exists")
3079
+
3080
+ module = self._design_module(kind, found.name)
3081
+ if module is not None:
3082
+ self._rename_module_streams(module, self.DESIGN_MODULE_PREFIX[kind] + new_name)
3083
+
3084
+ container = self._design_container(kind)
3085
+ storage = self.table(STORAGE_TABLE)
3086
+ for rid, row in list(storage.rows_with_ids()):
3087
+ value = row.get("Lv")
3088
+ if (
3089
+ isinstance(value, bytes)
3090
+ and value
3091
+ and str(row["Name"]) == DIR_DATA
3092
+ and _as_int(row["ParentId"]) == container
3093
+ ):
3094
+ storage.update_row(rid, {"Lv": rename_dir_data(value, found.name, new_name)})
3095
+ self._rename_catalog_rows(found.name, new_name, OBJECT_TYPES[kind])
3096
+ self.forget_catalog()
3097
+ self._drop_srp()
3098
+
2991
3099
  def delete_form(self, name: str) -> None:
2992
3100
  """Remove a form and every structure it occupies."""
2993
3101
  self._delete_design("form", name)
@@ -2998,6 +3106,13 @@ class AccessDatabase:
2998
3106
 
2999
3107
  def _delete_design(self, kind: str, name: str) -> None:
3000
3108
  found = self._design(kind, name)
3109
+ # The module behind it goes too, or the project is left naming a
3110
+ # DocClass whose design is gone, which Access reads as corrupt
3111
+ # (GitHub issue #21). It has to go first, while the design is
3112
+ # still there to name it.
3113
+ module = self._design_module(kind, found.name)
3114
+ if module is not None:
3115
+ self._delete_module_streams(module)
3001
3116
  container = self._design_container(kind)
3002
3117
  storage = self.table(STORAGE_TABLE)
3003
3118
  listing_rid, listing_payload = next(
@@ -4031,12 +4146,6 @@ class AccessDatabase:
4031
4146
  row_name, parent = str(row["Name"]), _as_int(row["ParentId"])
4032
4147
  if row_name == "_VBA_PROJECT":
4033
4148
  storage.update_row(rid, {"Lv": invalidate_cache(payload)})
4034
- elif row_name == "\x03DirData" and parent == modules_id:
4035
- # The four bytes an entry ends with name the object's
4036
- # storage folder, not a terminator.
4037
- storage.update_row(rid, {"Lv": add_to_dir_data(payload, name, folder)})
4038
- elif row_name == "PropData" and parent == modules_id:
4039
- storage.update_row(rid, {"Lv": add_to_folder_list(payload, folder)})
4040
4149
  elif row_name == "PROJECTwm" and parent == project_id:
4041
4150
  storage.update_row(rid, {"Lv": add_to_project_wm(payload, name, encoding)})
4042
4151
  elif row_name == "PROJECT" and parent == project_id:
@@ -4051,6 +4160,9 @@ class AccessDatabase:
4051
4160
  )
4052
4161
  },
4053
4162
  )
4163
+ # The four bytes an entry ends with name the object's storage
4164
+ # folder, not a terminator.
4165
+ self._list_in_container(modules_id, name, folder, when)
4054
4166
  storage.update_row(
4055
4167
  dir_rid,
4056
4168
  {"Lv": compress(add_to_dir(dir_stream, dir_block(name, stream_name, cookie, kind, encoding)))},
@@ -4098,16 +4210,16 @@ class AccessDatabase:
4098
4210
  self._invalidate_vba_cache()
4099
4211
  self._drop_srp()
4100
4212
 
4101
- def rename_module(self, name: str, new_name: str) -> None:
4102
- """Rename a module in all eight places its name lives."""
4103
- if not new_name or len(new_name) > 64:
4104
- raise AccessError("a module name is 1 to 64 characters")
4105
- module = self.module(name)
4106
- if new_name.lower() != name.lower() and any(
4107
- other.name.lower() == new_name.lower() for other in self.modules()
4108
- ):
4109
- raise AccessError(f"a module named {new_name!r} already exists")
4213
+ def _rename_module_streams(self, module: VBAModule, new_name: str) -> None:
4214
+ """Rename a module wherever the VBA project itself names it: the
4215
+ two dir records, the module's own `Attribute VB_Name`, PROJECT and
4216
+ PROJECTwm.
4110
4217
 
4218
+ The Modules listing is deliberately left alone here, because the
4219
+ code behind a form or report appears in neither container listing:
4220
+ the project carries `Form_Calculator` while the Forms listing
4221
+ carries `Calculator`.
4222
+ """
4111
4223
  storage = self.table(STORAGE_TABLE)
4112
4224
  dir_rid, dir_stream = self._vba_dir()
4113
4225
  encoding = encoding_of(dir_stream)
@@ -4120,7 +4232,7 @@ class AccessDatabase:
4120
4232
  stream = set_module_offset(rename_in_dir(dir_stream, module.name, new_name), new_name, 0)
4121
4233
  storage.update_row(dir_rid, {"Lv": compress(stream)})
4122
4234
 
4123
- modules_id, project_id, _streams = self._vba_storage_ids()
4235
+ _modules, project_id, _streams = self._vba_storage_ids()
4124
4236
  for row_rid, row in list(storage.rows_with_ids()):
4125
4237
  value = row.get("Lv")
4126
4238
  if not isinstance(value, bytes) or not value:
@@ -4128,8 +4240,6 @@ class AccessDatabase:
4128
4240
  row_name, parent = str(row["Name"]), _as_int(row["ParentId"])
4129
4241
  if row_name == "_VBA_PROJECT":
4130
4242
  storage.update_row(row_rid, {"Lv": invalidate_cache(value)})
4131
- elif row_name == "\x03DirData" and parent == modules_id:
4132
- storage.update_row(row_rid, {"Lv": rename_dir_data(value, module.name, new_name)})
4133
4243
  elif row_name == "PROJECTwm" and parent == project_id:
4134
4244
  storage.update_row(
4135
4245
  row_rid, {"Lv": rename_project_wm(value, module.name, new_name, encoding)}
@@ -4140,40 +4250,83 @@ class AccessDatabase:
4140
4250
  if fixed != text:
4141
4251
  storage.update_row(row_rid, {"Lv": encode_mbcs(fixed, encoding)})
4142
4252
 
4253
+ def _rename_catalog_rows(self, old: str, new: str, object_type: int) -> None:
4254
+ """The object's catalog row and its navigation-pane row."""
4143
4255
  objects = self.table("MSysObjects")
4144
4256
  for row_rid, row in objects.rows_with_ids():
4145
- if row["Type"] == OBJECT_MODULE and str(row["Name"]) == module.name:
4146
- objects.update_row(row_rid, {"Name": new_name})
4257
+ if row["Type"] == object_type and str(row["Name"]) == old:
4258
+ objects.update_row(row_rid, {"Name": new})
4147
4259
  break
4148
4260
  nav = self.table("MSysNavPaneObjectIDs")
4149
4261
  for row_rid, row in nav.rows_with_ids():
4150
- if str(row["Name"]) == module.name:
4151
- nav.update_row(row_rid, {"Name": new_name})
4262
+ if str(row["Name"]) == old:
4263
+ nav.update_row(row_rid, {"Name": new})
4152
4264
  break
4265
+
4266
+ def rename_module(self, name: str, new_name: str) -> None:
4267
+ """Rename a module in all eight places its name lives."""
4268
+ if not new_name or len(new_name) > 64:
4269
+ raise AccessError("a module name is 1 to 64 characters")
4270
+ module = self.module(name)
4271
+ if new_name.lower() != name.lower() and any(
4272
+ other.name.lower() == new_name.lower() for other in self.modules()
4273
+ ):
4274
+ raise AccessError(f"a module named {new_name!r} already exists")
4275
+
4276
+ self._rename_module_streams(module, new_name)
4277
+ storage = self.table(STORAGE_TABLE)
4278
+ modules_id = self._vba_storage_ids()[0]
4279
+ for row_rid, row in list(storage.rows_with_ids()):
4280
+ value = row.get("Lv")
4281
+ if (
4282
+ isinstance(value, bytes)
4283
+ and value
4284
+ and str(row["Name"]) == DIR_DATA
4285
+ and _as_int(row["ParentId"]) == modules_id
4286
+ ):
4287
+ storage.update_row(row_rid, {"Lv": rename_dir_data(value, module.name, new_name)})
4288
+ self._rename_catalog_rows(module.name, new_name, OBJECT_MODULE)
4153
4289
  self._drop_srp()
4154
4290
  self._catalog = None
4155
4291
 
4156
4292
  def delete_module(self, name: str) -> None:
4157
4293
  """Remove a module and every structure it occupies."""
4158
4294
  module = self.module(name)
4159
- index = [m.name for m in self.modules()].index(module.name)
4160
4295
  encoding = encoding_of(self._vba_dir()[1])
4161
4296
  storage = self.table(STORAGE_TABLE)
4162
4297
  modules_id, project_id, streams_id = self._vba_storage_ids()
4163
4298
 
4164
- # A module's storage folder is linked to it by position, not by
4165
- # name: deleting the second of three modules, Access dropped the
4166
- # second folder and left the others named as they were.
4167
- folders = sorted(
4299
+ # The container's own DirData names each module's storage folder,
4300
+ # the way `_delete_design` reads it. This used to take the folder
4301
+ # by position among `modules()`, which is dir-stream order and
4302
+ # includes the module behind a form or report. Those have no
4303
+ # folder here at all, so the two lists were different lengths and
4304
+ # every module after a document one sat a place high: deleting one
4305
+ # took another's folder, or ran off the end (GitHub issue #19).
4306
+ listing = next(
4168
4307
  (
4169
- (rid, row)
4170
- for rid, row in storage.rows_with_ids()
4171
- if _as_int(row["ParentId"]) == modules_id and _as_int(row["Type"]) == 1
4308
+ payload
4309
+ for _rid, row in storage.rows_with_ids()
4310
+ if _as_int(row["ParentId"]) == modules_id
4311
+ and str(row["Name"]) == DIR_DATA
4312
+ and isinstance(payload := row.get("Lv"), bytes)
4172
4313
  ),
4173
- key=lambda pair: _as_int(pair[1]["Id"]),
4314
+ None,
4315
+ )
4316
+ entries = dict(dir_data_entries(listing)) if listing is not None else {}
4317
+ if module.name not in entries:
4318
+ raise AccessError(
4319
+ f"{module.name!r} is the code behind a form or report, which has no "
4320
+ "storage folder of its own; delete the design instead"
4321
+ )
4322
+ folder_name = entries[module.name]
4323
+ folder_rid, folder_id = next(
4324
+ (rid, _as_int(row["Id"]))
4325
+ for rid, row in storage.rows_with_ids()
4326
+ if _as_int(row["ParentId"]) == modules_id
4327
+ and _as_int(row["Type"]) == TYPE_FOLDER
4328
+ and str(row["Name"]) == folder_name
4174
4329
  )
4175
- folder_rid, folder_row = folders[index]
4176
- folder_name, folder_id = str(folder_row["Name"]), _as_int(folder_row["Id"])
4177
4330
  doomed = [folder_rid] + [
4178
4331
  rid for rid, row in storage.rows_with_ids() if _as_int(row["ParentId"]) == folder_id
4179
4332
  ]
File without changes
File without changes
File without changes
File without changes