|
7 | 7 |
|
8 | 8 | from __future__ import annotations |
9 | 9 |
|
10 | | -import collections |
11 | 10 | import inspect |
12 | | -import itertools |
13 | 11 | import logging |
14 | 12 | import re |
15 | 13 | import types |
@@ -516,83 +514,6 @@ def jobs(self) -> list[Job]: |
516 | 514 |
|
517 | 515 | return jobs_list |
518 | 516 |
|
519 | | - @property |
520 | | - def code(self): |
521 | | - self._assert_exists() |
522 | | - return self.save() |
523 | | - |
524 | | - def save(self, python_filename: str | None = None) -> str: |
525 | | - """ |
526 | | - Generate Python code that recreates this schema. |
527 | | -
|
528 | | - Parameters |
529 | | - ---------- |
530 | | - python_filename : str, optional |
531 | | - If provided, write the code to this file. |
532 | | -
|
533 | | - Returns |
534 | | - ------- |
535 | | - str |
536 | | - Python module source code defining this schema. |
537 | | -
|
538 | | - Notes |
539 | | - ----- |
540 | | - This method is in preparation for a future release and is not |
541 | | - officially supported. |
542 | | - """ |
543 | | - self.connection.dependencies.load() |
544 | | - self._assert_exists() |
545 | | - module_count = itertools.count() |
546 | | - # add virtual modules for referenced modules with names vmod0, vmod1, ... |
547 | | - module_lookup = collections.defaultdict(lambda: "vmod" + str(next(module_count))) |
548 | | - db = self.database |
549 | | - |
550 | | - def make_class_definition(table): |
551 | | - tier = _get_tier(table).__name__ |
552 | | - class_name = self.connection.adapter.split_full_table_name(table)[1] |
553 | | - indent = "" |
554 | | - if tier == "Part": |
555 | | - class_name = class_name.split("__")[-1] |
556 | | - indent += " " |
557 | | - class_name = to_camel_case(class_name) |
558 | | - |
559 | | - def replace(s): |
560 | | - d, tabs = s.group(1), s.group(2) |
561 | | - return ("" if d == db else (module_lookup[d] + ".")) + ".".join( |
562 | | - to_camel_case(tab) for tab in tabs.lstrip("__").split("__") |
563 | | - ) |
564 | | - |
565 | | - return ("" if tier == "Part" else "\n@schema\n") + ( |
566 | | - '{indent}class {class_name}(dj.{tier}):\n{indent} definition = """\n{indent} {defi}"""' |
567 | | - ).format( |
568 | | - class_name=class_name, |
569 | | - indent=indent, |
570 | | - tier=tier, |
571 | | - defi=re.sub( |
572 | | - r"`([^`]+)`.`([^`]+)`", |
573 | | - replace, |
574 | | - FreeTable(self.connection, table).describe(), |
575 | | - ).replace("\n", "\n " + indent), |
576 | | - ) |
577 | | - |
578 | | - tables = self.connection.dependencies.topo_sort() |
579 | | - body = "\n\n".join(make_class_definition(table) for table in tables) |
580 | | - python_code = "\n\n".join( |
581 | | - ( |
582 | | - '"""This module was auto-generated by datajoint from an existing schema"""', |
583 | | - "import datajoint as dj\n\nschema = dj.Schema('{db}')".format(db=db), |
584 | | - "\n".join( |
585 | | - "{module} = dj.VirtualModule('{module}', '{schema_name}')".format(module=v, schema_name=k) |
586 | | - for k, v in module_lookup.items() |
587 | | - ), |
588 | | - body, |
589 | | - ) |
590 | | - ) |
591 | | - if python_filename is None: |
592 | | - return python_code |
593 | | - with open(python_filename, "wt") as f: |
594 | | - f.write(python_code) |
595 | | - |
596 | 517 | def list_tables(self) -> list[str]: |
597 | 518 | """ |
598 | 519 | Return all user tables in the schema. |
|
0 commit comments