forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
694 lines (552 loc) · 20.4 KB
/
utils.py
File metadata and controls
694 lines (552 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
import inspect
import os
import re
import shutil
import socket
from dataclasses import dataclass
from os.path import join
from urllib.parse import unquote, urlparse
import frappe
from frappe.model.document import Document
from frappe.modules.import_file import import_file_by_path
from frappe.utils import get_url
from frappe.utils.html_utils import unescape_html
from frappe.utils.safe_exec import (
SERVER_SCRIPT_FILE_PREFIX,
FrappeTransformer,
NamespaceDict,
get_python_builtins,
get_safe_globals,
is_safe_exec_enabled,
safe_exec,
safe_exec_flags,
)
from RestrictedPython import compile_restricted
from RestrictedPython import safe_globals as restricted_safe_globals
from werkzeug.routing import Rule
@dataclass
class BlockDataKey:
key: str
property: str
type: str
comesFrom: str
class VisibilityCondition:
key: str
comesFrom: str
class Block:
blockId: str = ""
from typing import ClassVar
children: ClassVar[list["Block"]] = []
baseStyles: ClassVar[dict] = {}
rawStyles: ClassVar[dict] = {}
mobileStyles: ClassVar[dict] = {}
tabletStyles: ClassVar[dict] = {}
attributes: ClassVar[dict] = {}
classes: ClassVar[list[str]] = []
dataKey: BlockDataKey | None = None
blockName: str | None = None
element: str | None = None
draggable: bool = False
innerText: str | None = None
innerHTML: str | None = None
extendedFromComponent: str | None = None
originalElement: str | None = None
isChildOfComponent: str | None = None
referenceBlockId: str | None = None
isRepeaterBlock: bool = False
visibilityCondition: str | VisibilityCondition | None = None
elementBeforeConversion: str | None = None
customAttributes: ClassVar[dict] = {}
dynamicValues: ClassVar[list[BlockDataKey]] = []
blockClientScript: str = ""
blockDataScript: str = ""
props: ClassVar[dict] = {}
def __init__(self, **kwargs) -> None:
for key, value in kwargs.items():
if key == "children":
value = [
b if isinstance(b, Block) else Block(**b) if b and isinstance(b, dict) else None
for b in (value or [])
]
setattr(self, key, value)
def set_dynamic_value(self, key: str, type: str, property: str, comesFrom: str = "dataScript"):
if not self.dynamicValues:
self.dynamicValues = []
for i, dv in enumerate(self.dynamicValues):
if dv["property"] == property and dv["type"] == type:
self.dynamicValues[i] = {
"key": key,
"type": type,
"property": property,
"comesFrom": comesFrom,
}
return
self.dynamicValues.append({"key": key, "type": type, "property": property, "comesFrom": comesFrom})
def clear_dynamic_values(self):
self.dynamicValues = []
def attach_data_key(self, key: str, property: str, type: str = "key", comesFrom: str = "dataScript"):
self.dataKey = {"key": key, "property": property, "type": type, "comesFrom": comesFrom}
def clear_data_key(self):
self.dataKey = None
def attach_children(self, *children: "Block"):
if not self.children:
self.children = []
self.children.extend(children)
def as_dict(self):
return {
"blockId": self.blockId,
"children": [child.as_dict() for child in self.children] if self.children else None,
"baseStyles": self.baseStyles,
"rawStyles": self.rawStyles,
"mobileStyles": self.mobileStyles,
"tabletStyles": self.tabletStyles,
"attributes": self.attributes,
"classes": self.classes,
"dataKey": self.dataKey,
"blockName": self.blockName,
"element": self.element,
"draggable": self.draggable,
"innerText": self.innerText,
"innerHTML": self.innerHTML,
"extendedFromComponent": self.extendedFromComponent,
"originalElement": self.originalElement,
"isChildOfComponent": self.isChildOfComponent,
"referenceBlockId": self.referenceBlockId,
"isRepeaterBlock": self.isRepeaterBlock,
"visibilityCondition": self.visibilityCondition,
"elementBeforeConversion": self.elementBeforeConversion,
"customAttributes": self.customAttributes,
"dynamicValues": self.dynamicValues,
"blockClientScript": self.blockClientScript,
"blockDataScript": self.blockDataScript,
"props": self.props,
}
def as_json(self, wrap_in_array=False):
return frappe.as_json([self.as_dict()]) if wrap_in_array else frappe.as_json(self.as_dict())
def get_doc_as_dict(doctype, name):
assert isinstance(doctype, str)
assert isinstance(name, str)
return frappe.get_doc(doctype, name).as_dict()
def get_cached_doc_as_dict(doctype, name):
assert isinstance(doctype, str)
assert isinstance(name, str)
return frappe.get_cached_doc(doctype, name).as_dict()
def make_safe_get_request(url, **kwargs):
parsed = urlparse(url)
parsed_ip = socket.gethostbyname(parsed.hostname)
if parsed_ip.startswith(("127", "10", "192", "172")):
return
return frappe.integrations.utils.make_get_request(url, **kwargs)
def safe_get_list(*args, **kwargs):
if args and len(args) > 1 and isinstance(args[1], list):
args = list(args)
args[1] = remove_unsafe_fields(args[1])
fields = kwargs.get("fields", [])
if fields:
kwargs["fields"] = remove_unsafe_fields(fields)
return frappe.db.get_list(
*args,
**kwargs,
)
def safe_get_all(*args, **kwargs):
kwargs["ignore_permissions"] = True
if "limit_page_length" not in kwargs:
kwargs["limit_page_length"] = 0
return safe_get_list(*args, **kwargs)
def remove_unsafe_fields(fields):
return [f for f in fields if "(" not in f]
def get_safer_globals():
safe_globals = get_safe_globals()
form_dict = getattr(frappe.local, "form_dict", frappe._dict())
if "_" in form_dict:
del frappe.local.form_dict["_"]
out = NamespaceDict(
json=safe_globals["json"],
as_json=frappe.as_json,
dict=safe_globals["dict"],
args=form_dict,
frappe=NamespaceDict(
db=NamespaceDict(
count=frappe.db.count,
exists=frappe.db.exists,
get_all=safe_get_all,
get_list=safe_get_list,
get_single_value=frappe.db.get_single_value,
),
form_dict=form_dict,
make_get_request=make_safe_get_request,
get_doc=get_doc_as_dict,
get_cached_doc=get_cached_doc_as_dict,
_=frappe._,
session=safe_globals["frappe"]["session"],
),
)
out._write_ = safe_globals["_write_"]
out._getitem_ = safe_globals["_getitem_"]
out._getattr_ = safe_globals["_getattr_"]
out._getiter_ = safe_globals["_getiter_"]
out._iter_unpack_sequence_ = safe_globals["_iter_unpack_sequence_"]
# add common python builtins
out.update(restricted_safe_globals)
out.update(get_python_builtins())
return out
def safer_exec(
script: str,
_globals: dict | None = None,
_locals: dict | None = None,
*,
script_filename: str | None = None,
):
exec_globals = get_safer_globals()
if _globals:
exec_globals.update(_globals)
filename = SERVER_SCRIPT_FILE_PREFIX
if script_filename:
filename += f": {frappe.scrub(script_filename)}"
with safe_exec_flags():
# execute script compiled by RestrictedPython
exec(
compile_restricted(script, filename=filename, policy=FrappeTransformer),
exec_globals,
_locals,
)
return exec_globals, _locals
def sync_page_templates():
print("Syncing Builder Components")
builder_component_path = frappe.get_module_path("builder", "builder_component")
make_records(builder_component_path)
print("Syncing Builder Scripts")
builder_script_path = frappe.get_module_path("builder", "builder_script")
make_records(builder_script_path)
print("Syncing Builder Page Templates")
builder_page_template_path = frappe.get_module_path("builder", "builder_page_template")
make_records(builder_page_template_path)
def sync_block_templates():
print("Syncing Builder Block Templates")
builder_block_template_path = frappe.get_module_path("builder", "builder_block_template")
make_records(builder_block_template_path)
def sync_builder_variables():
print("Syncing Builder Builder Variables")
builder_variable_path = frappe.get_module_path("builder", "builder_variable")
make_records(builder_variable_path)
def make_records(path):
if not os.path.isdir(path):
return
for fname in os.listdir(path):
if os.path.isdir(join(path, fname)) and fname != "__pycache__":
import_file_by_path(f"{path}/{fname}/{fname}.json")
# def generate_tailwind_css_file_from_html(html):
# # execute tailwindcss cli command to generate css file
# # create temp folder
# temp_folder = os.path.join(get_site_base_path(), "temp")
# if os.path.exists(temp_folder):
# shutil.rmtree(temp_folder)
# os.mkdir(temp_folder)
# # create temp html file
# temp_html_file_path = os.path.join(temp_folder, "temp.html")
# with open(temp_html_file_path, "w") as f:
# f.write(html)
# # place tailwind.css file in public folder
# tailwind_css_file_path = os.path.join(get_site_path(), "public", "files", "tailwind.css")
# # create temp config file
# temp_config_file_path = os.path.join(temp_folder, "tailwind.config.js")
# with open(temp_config_file_path, "w") as f:
# f.write("module.exports = {content: ['./temp.html']}")
# # run tailwindcss cli command in production mode
# subprocess.run(
# ["npx", "tailwindcss", "-o", tailwind_css_file_path, "--config", temp_config_file_path, "--minify"]
# )
def copy_img_to_asset_folder(block, page_doc):
def safe_get(obj, attr, default=None):
if isinstance(obj, dict):
return obj.get(attr, default)
else:
return getattr(obj, attr, default)
if isinstance(block, dict):
block = frappe._dict(block)
children = block.get("children", [])
if children and isinstance(children, list):
block.children = [frappe._dict(child) if isinstance(child, dict) else child for child in children]
element = safe_get(block, "element")
if element == "img":
attributes = safe_get(block, "attributes")
src = None
if attributes:
src = safe_get(attributes, "src")
site_url = get_url()
if src and (src.startswith(f"{site_url}/files") or src.startswith("/files")):
if src.startswith(f"{site_url}/files"):
src = src.split(f"{site_url}")[1]
src = unquote(src)
files = frappe.get_all("File", filters={"file_url": src}, fields=["name"])
if files:
_file = frappe.get_doc("File", files[0].name)
assets_folder_path = get_template_assets_folder_path(page_doc)
shutil.copy(_file.get_full_path(), assets_folder_path)
new_src = f"/builder_assets/{page_doc.name}/{src.split('/')[-1]}"
if attributes:
if isinstance(attributes, dict):
attributes["src"] = new_src
else:
attributes.src = new_src
children = safe_get(block, "children", [])
for child in children or []:
copy_img_to_asset_folder(child, page_doc)
def get_template_assets_folder_path(page_doc):
path = os.path.join(frappe.get_app_path("builder"), "www", "builder_assets", page_doc.name)
if not os.path.exists(path):
os.makedirs(path)
return path
def get_builder_page_preview_file_paths(page_doc):
public_path, public_path = None, None
if page_doc.is_template:
local_path = os.path.join(get_template_assets_folder_path(page_doc), "preview.webp")
public_path = f"/builder_assets/{page_doc.name}/preview.webp"
else:
file_name = f"{page_doc.name}-preview.webp"
local_path = os.path.join(frappe.local.site_path, "public", "files", file_name)
random_hash = frappe.generate_hash(length=5)
public_path = f"/files/{file_name}?v={random_hash}"
return public_path, local_path
def is_component_used(blocks, component_id):
blocks = frappe.parse_json(blocks)
if not isinstance(blocks, list):
blocks = [blocks]
for block in blocks:
if not block:
continue
if block.get("extendedFromComponent") == component_id:
return True
elif block.get("children"):
return is_component_used(block.get("children"), component_id)
return False
def escape_single_quotes(text):
return (text or "").replace("'", "\\'")
def camel_case_to_kebab_case(text, remove_spaces=False):
# Used to convert camelCase css properties to kebab-case, e.g. backgroundColor → background-color
if not text:
return ""
text = re.sub(r"(?<!^)(?=[A-Z])", "-", text).lower()
# Add leading hyphen for vendor-prefixed CSS properties
# e.g. WebkitBackgroundClip → -webkit-background-clip
if re.match(r"^(webkit|moz|ms|o)-", text):
text = f"-{text}"
if remove_spaces:
text = text.replace(" ", "")
return text
def sanitize_style_value(value):
if not isinstance(value, str):
return value
if value.count("(") != value.count(")"):
value = value.replace("(", "\\(").replace(")", "\\)")
if value.count("'") % 2 != 0:
value = value.replace("'", "\\'")
if value.count('"') % 2 != 0:
value = value.replace('"', '\\"')
return value
def execute_script(script, _locals, script_filename):
if is_safe_exec_enabled():
safe_exec(script, None, _locals, script_filename=script_filename)
else:
safer_exec(script, None, _locals, script_filename=script_filename)
def get_dummy_blocks():
return [
{
"element": "div",
"extendedFromComponent": "component-1",
"children": [
{
"element": "div",
"children": [
{
"element": "div",
"extendedFromComponent": "component-2",
"children": [],
},
],
},
],
},
]
def clean_data(data):
if isinstance(data, dict):
return {
k: clean_data(v)
for k, v in data.items()
if not inspect.isbuiltin(v) and not inspect.isfunction(v) and not inspect.ismethod(v)
}
elif isinstance(data, list):
return [clean_data(i) for i in data]
return data
class ColonRule(Rule):
def __init__(self, string, *args, **kwargs):
# Replace ':name' with '<name>' so Werkzeug can process it
string = self.convert_colon_to_brackets(string)
super().__init__(string, *args, **kwargs)
@staticmethod
def convert_colon_to_brackets(string):
return re.sub(r":([a-zA-Z0-9_-]+)", r"<\1>", string)
def add_composite_index_to_web_page_view():
"""
Add a composite index to the Web Page View table.
This is used to speed up queries that filter by creation, is_unique, and path.
"""
frappe.db.add_index("Web Page View", ["creation", "is_unique", "path"])
def split_styles(styles):
if not styles:
return {"regular": {}, "state": {}}
return {
"regular": {k: v for k, v in styles.items() if ":" not in k},
"state": {k: v for k, v in styles.items() if ":" in k},
}
def copy_assets_from_blocks(blocks, assets_path, target_app="builder"):
if not isinstance(blocks, list):
blocks = [blocks]
for block in blocks:
if isinstance(block, dict):
process_block_assets(block, assets_path, target_app)
children = block.get("children")
if children and isinstance(children, list):
copy_assets_from_blocks(children, assets_path, target_app)
def process_block_assets(block, assets_path, target_app="builder"):
"""Process assets for a single block"""
if block.get("element") in ("img", "video"):
src = block.get("attributes", {}).get("src")
if src:
new_location = copy_asset_file(src, assets_path, target_app)
if new_location:
block["attributes"]["src"] = new_location
def copy_asset_file(file_url, assets_path, target_app="builder"):
"""Copy a file from the source to assets directory and return new public path"""
if not file_url or not isinstance(file_url, str):
return None
try:
if file_url.startswith("/files/"):
return copy_from_site_files(file_url, assets_path, target_app)
elif file_url.startswith("/builder_assets/") or (
file_url.startswith("/assets/") and "/builder_assets/" in file_url
):
return copy_from_builder_assets(file_url, assets_path, target_app)
except Exception as e:
frappe.log_error(f"Failed to copy asset {file_url}: {e!s}")
return None
def copy_from_site_files(file_url, assets_path, target_app="builder"):
"""Copy file from site files directory"""
source_path = os.path.join(frappe.local.site_path, "public", file_url.lstrip("/"))
if os.path.exists(source_path):
return copy_file_to_assets(source_path, file_url, assets_path, target_app)
return None
def copy_from_builder_assets(file_url, assets_path, target_app="builder"):
"""Copy file from builder assets directory"""
if file_url.startswith("/assets/") and "/builder_assets/" in file_url:
parts = file_url.split("/")
if len(parts) >= 3:
app_name = parts[2]
source_path = os.path.join(frappe.get_app_path(app_name), "public", "/".join(parts[3:]))
else:
source_path = os.path.join(frappe.get_app_path("builder"), "www", file_url.lstrip("/"))
if os.path.exists(source_path):
return copy_file_to_assets(source_path, file_url, assets_path, target_app)
return None
def copy_file_to_assets(source_path, file_url, assets_path, target_app="builder"):
"""Copy file to assets directory and return public path"""
filename = os.path.basename(file_url)
dest_path = os.path.join(assets_path, filename)
shutil.copy2(source_path, dest_path)
return f"/assets/{target_app}/builder_assets/{filename}"
def extract_components_from_blocks(blocks):
"""Extract component IDs from blocks recursively"""
components = set()
if not isinstance(blocks, list):
blocks = [blocks]
for block in blocks:
if isinstance(block, dict):
if block.get("extendedFromComponent"):
component_doc = frappe.get_cached_doc("Builder Component", block["extendedFromComponent"])
if component_doc:
components.update(
extract_components_from_blocks(frappe.parse_json(component_doc.block or "{}"))
)
components.add(block["extendedFromComponent"])
children = block.get("children")
if children and isinstance(children, list):
components.update(extract_components_from_blocks(children))
return components
def export_client_scripts(page_doc, client_scripts_path):
"""Export client scripts for a page"""
from frappe.modules.export_file import strip_default_fields
for script_row in page_doc.client_scripts:
script_doc = frappe.get_doc("Builder Client Script", script_row.builder_script)
script_config = script_doc.as_dict(no_nulls=True)
script_config = strip_default_fields(script_doc, script_config)
fname = frappe.scrub(str(script_doc.name))
# ensure the target directory exists before writing the file
script_dir = os.path.join(client_scripts_path, fname)
os.makedirs(script_dir, exist_ok=True)
script_file_path = os.path.join(script_dir, f"{fname}.json")
with open(script_file_path, "w", encoding="utf-8") as f:
f.write(frappe.as_json(script_config, ensure_ascii=False))
def export_components(components, components_path, assets_path, target_app="builder"):
"""Export components to files"""
for component_id in components:
try:
component_doc = frappe.get_doc("Builder Component", component_id)
# replace assets in component blocks
component_blocks = frappe.parse_json(component_doc.block or "[]")
copy_assets_from_blocks(component_blocks, assets_path, target_app)
component_doc.block = frappe.as_json(component_blocks)
# Replace forward slashes with underscores to create valid directory names
safe_component_name = frappe.scrub(component_doc.component_name).replace("/", "_")
component_dir = os.path.join(components_path, safe_component_name)
os.makedirs(component_dir, exist_ok=True)
component_file_path = os.path.join(component_dir, f"{safe_component_name}.json")
with open(component_file_path, "w") as f:
f.write(frappe.as_json(component_doc.as_dict()))
except Exception as e:
print(e)
frappe.log_error(f"Failed to export component {component_id}: {e!s}")
def create_export_directories(app_path, export_name):
paths = get_export_paths(app_path, export_name)
for path in paths.values():
os.makedirs(path, exist_ok=True)
return paths
def get_export_paths(app_path, export_name):
"""Get all export directory paths"""
builder_files_path = os.path.join(app_path, "builder_files")
pages_path = os.path.join(builder_files_path, "pages")
public_builder_files_path = os.path.join(app_path, "public", "builder_assets")
return {
"page_path": os.path.join(pages_path, export_name),
"assets_path": public_builder_files_path,
"client_scripts_path": os.path.join(builder_files_path, "client_scripts"),
"components_path": os.path.join(builder_files_path, "components"),
"builder_files_path": builder_files_path,
"pages_path": pages_path,
}
def to_dict_with_fallback(obj):
try:
return frappe._dict(obj)
except TypeError:
if isinstance(obj, Document):
return obj.as_dict()
else:
raise
def combine(a, b):
if a is None:
return b
if b is None:
return a
res = to_dict_with_fallback(a)
res.update(to_dict_with_fallback(b))
return res
def hash(s):
return f"{frappe.generate_hash(length=6)}-{s}"
def to_safe_json(data):
return frappe.as_json(data or {})
def execute_script_and_combine(prev_block_data, block_data_script, props):
props = frappe._dict(frappe.parse_json(props or "{}"))
block_data = frappe._dict()
_locals = dict(block=frappe._dict(), prev_blocks=to_dict_with_fallback(prev_block_data), props=props)
execute_script(unescape_html(block_data_script), _locals, "sample")
block_data.update(_locals["block"])
return combine(prev_block_data, block_data)