|
| 1 | +import json |
| 2 | + |
| 3 | +from contentstack_utils.helper.converter import convert_style |
| 4 | +from contentstack_utils.helper.metadata import Metadata |
| 5 | +from contentstack_utils.helper.node_to_html import NodeToHtml |
| 6 | +from contentstack_utils.render.options import Options |
| 7 | + |
| 8 | + |
| 9 | +class Automate: |
| 10 | + |
| 11 | + @staticmethod |
| 12 | + def _str_from_embed_items(metadata, entry, option): |
| 13 | + if isinstance(entry, list): |
| 14 | + for node in entry: |
| 15 | + uid = node['node']['uid'] |
| 16 | + if uid == metadata.get_item_uid: |
| 17 | + return option.render_options(node['node'], metadata) |
| 18 | + elif isinstance(entry, dict) and '_embedded_items' in entry: |
| 19 | + items = entry['_embedded_items'].keys() |
| 20 | + for item in items: |
| 21 | + items_array = entry['_embedded_items'][item] |
| 22 | + content = Automate._find_embedded_entry(items_array, metadata) |
| 23 | + if content is not None: |
| 24 | + return option.render_options(content, metadata) |
| 25 | + return '' |
| 26 | + |
| 27 | + @staticmethod |
| 28 | + def _get_embedded_keys(entry, key_path, option: Options, render_callback): |
| 29 | + if '_embedded_items' in entry: |
| 30 | + if key_path is not None: |
| 31 | + for path in key_path: |
| 32 | + Automate._find_embed_keys(entry, path, option, render_callback) |
| 33 | + else: |
| 34 | + _embedded_items = entry['_embedded_items'] |
| 35 | + available_keys: list = _embedded_items.keys() |
| 36 | + for path in available_keys: |
| 37 | + Automate._find_embed_keys(entry, path, option, render_callback) |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def _find_embed_keys(entry, path, option: Options, render_callback): |
| 41 | + keys = path.split('.') |
| 42 | + Automate._get_content(keys, entry, option, render_callback) |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def _get_content(keys_array, entry, option: Options, render_callback): |
| 46 | + if keys_array is not None and len(keys_array) > 0: |
| 47 | + key = keys_array[0] |
| 48 | + if len(keys_array) == 1 and keys_array[0] in entry: |
| 49 | + var_content = entry[key] |
| 50 | + if isinstance(var_content, (list, str, dict)): |
| 51 | + entry[key] = render_callback(var_content, entry, option) |
| 52 | + else: |
| 53 | + keys_array.remove(key) |
| 54 | + if key in entry and isinstance(entry[key], dict): |
| 55 | + Automate._get_content(keys_array, entry[key], option, render_callback) |
| 56 | + elif key in entry and isinstance(entry[key], list): |
| 57 | + list_json = entry[key] |
| 58 | + for node in list_json: |
| 59 | + Automate._get_content(keys_array, node, option, render_callback) |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + def is_json(self: object) -> bool: |
| 63 | + try: |
| 64 | + json.dumps(self) |
| 65 | + return True |
| 66 | + except ValueError: |
| 67 | + return False |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def find_embed_keys(entry, path, option: Options, render_callback): |
| 71 | + keys = path.split('.') |
| 72 | + Automate.get_content(keys, entry, option, render_callback) |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def get_content(keys_array, entry, option: Options, render_callback): |
| 76 | + if keys_array is not None and len(keys_array) > 0: |
| 77 | + key = keys_array[0] |
| 78 | + if len(keys_array) == 1 and keys_array[0] in entry: |
| 79 | + var_content = entry[key] |
| 80 | + if isinstance(var_content, (list, str, dict)): |
| 81 | + entry[key] = render_callback(var_content, entry, option) |
| 82 | + else: |
| 83 | + keys_array.remove(key) |
| 84 | + if key in entry and isinstance(entry[key], dict): |
| 85 | + Automate.get_content(keys_array, entry[key], option, render_callback) |
| 86 | + elif key in entry and isinstance(entry[key], list): |
| 87 | + list_json = entry[key] |
| 88 | + for node in list_json: |
| 89 | + Automate.get_content(keys_array, node, option, render_callback) |
| 90 | + |
| 91 | + @staticmethod |
| 92 | + def _enumerate_content(content, entry, option): |
| 93 | + if len(content) > 0: |
| 94 | + if isinstance(content, list): |
| 95 | + array_content = [] |
| 96 | + for item in content: |
| 97 | + result = Automate._enumerate_content(item, entry, option) |
| 98 | + array_content.append(result) |
| 99 | + return array_content |
| 100 | + if isinstance(content, dict): |
| 101 | + if 'type' and 'children' in content: |
| 102 | + if content['type'] == 'doc': |
| 103 | + return Automate._raw_processing(content['children'], entry, option) |
| 104 | + return '' |
| 105 | + |
| 106 | + @staticmethod |
| 107 | + def _raw_processing(children, entry, option): |
| 108 | + array_container = [] |
| 109 | + for item in children: |
| 110 | + if isinstance(item, dict): |
| 111 | + array_container.append(Automate._extract_keys(item, entry, option)) |
| 112 | + temp = ''.join(array_container) |
| 113 | + return temp |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + def _extract_keys(item, entry, option: Options): |
| 117 | + if 'type' not in item.keys() and 'text' in item.keys(): |
| 118 | + return NodeToHtml.text_node_to_html(item, option) |
| 119 | + |
| 120 | + elif 'type' in item.keys(): |
| 121 | + node_style = item['type'] |
| 122 | + if node_style == 'reference': |
| 123 | + metadata = Automate._return_metadata(item, node_style) |
| 124 | + return Automate._str_from_embed_items(metadata=metadata, entry=entry, option=option) |
| 125 | + else: |
| 126 | + def call(children): |
| 127 | + return Automate._raw_processing(children, entry, option) |
| 128 | + |
| 129 | + return option.render_node(node_style, item, callback=call) |
| 130 | + return '' |
| 131 | + |
| 132 | + @staticmethod |
| 133 | + def _find_embedded_entry(list_json: list, metadata: Metadata): |
| 134 | + for obj in list_json: |
| 135 | + if obj['uid'] == metadata.get_item_uid: |
| 136 | + return obj |
| 137 | + return None |
| 138 | + |
| 139 | + @staticmethod |
| 140 | + def _return_metadata(item, node_style): |
| 141 | + attr = item['attrs'] |
| 142 | + text = Automate._get_child_text(item) |
| 143 | + style = convert_style(attr['display-type']) |
| 144 | + if attr['type'] == 'asset': |
| 145 | + return Metadata(text, node_style, |
| 146 | + attr['asset-uid'], |
| 147 | + 'sys-asset', |
| 148 | + style, '', '') |
| 149 | + else: |
| 150 | + return Metadata(text, node_style, |
| 151 | + attr['entry-uid'], |
| 152 | + attr['content-type-uid'], |
| 153 | + style, '', '') |
| 154 | + |
| 155 | + @staticmethod |
| 156 | + def _get_child_text(item): |
| 157 | + text = '' |
| 158 | + if 'children' in item.keys() and len(item['children']) > 0: |
| 159 | + children = item['children'] |
| 160 | + for child in children: |
| 161 | + if text in child.keys(): |
| 162 | + text = child['text'] |
| 163 | + break |
| 164 | + return text |
0 commit comments