dfpyre 0.7.11__py3-none-any.whl → 0.8.0__py3-none-any.whl

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.

Potentially problematic release.


This version of dfpyre might be problematic. Click here for more details.

dfpyre/scriptgen.py CHANGED
@@ -3,12 +3,10 @@ import re
3
3
  from dfpyre.items import *
4
4
  from dfpyre.actiondump import get_default_tags
5
5
 
6
- SCRIPT_START = '''from dfpyre import *
7
6
 
8
- t = DFTemplate()
9
- '''
7
+ SCRIPT_START = '''from dfpyre import *\n\n'''
10
8
 
11
- TEMPLATE_METHOD_LOOKUP = {
9
+ TEMPLATE_FUNCTION_LOOKUP = {
12
10
  'event': 'player_event',
13
11
  'entity_event': 'entity_event',
14
12
  'func': 'function',
@@ -30,6 +28,7 @@ TEMPLATE_METHOD_LOOKUP = {
30
28
  }
31
29
 
32
30
  TARGET_CODEBLOCKS = {'player_action', 'entity_action', 'if_player', 'if_entity'}
31
+ CONTAINER_CODEBLOCKS = {'event', 'entity_event', 'func', 'process', 'if_player', 'if_entity', 'if_game', 'if_variable', 'else', 'repeat'}
33
32
  VAR_SCOPES = {'unsaved': 'g', 'saved': 's', 'local': 'l', 'line': 'i'}
34
33
 
35
34
 
@@ -40,7 +39,7 @@ class GeneratorFlags:
40
39
  var_shorthand: bool
41
40
 
42
41
 
43
- def item_to_string(class_name: str, i: item):
42
+ def item_to_string(class_name: str, i: Item):
44
43
  i.nbt.data.pop('~DF_NBT', None)
45
44
  stripped_id = i.get_id().replace('minecraft:', '')
46
45
  if i.nbt.key_set() == {'~id', '~count'}:
@@ -52,27 +51,27 @@ def item_to_string(class_name: str, i: item):
52
51
 
53
52
  def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
54
53
  class_name = arg_item.__class__.__name__
55
- if isinstance(arg_item, item):
54
+ if isinstance(arg_item, Item):
56
55
  return item_to_string(class_name, arg_item)
57
56
 
58
- if isinstance(arg_item, string):
57
+ if isinstance(arg_item, String):
59
58
  value = arg_item.value.replace('\n', '\\n')
60
59
  return f'{class_name}("{value}")'
61
60
 
62
- if isinstance(arg_item, text):
61
+ if isinstance(arg_item, Text):
63
62
  value = arg_item.value.replace('\n', '\\n')
64
63
  if flags.literal_shorthand:
65
64
  return f'"{value}"'
66
65
  return f'{class_name}("{value}")'
67
66
 
68
- if isinstance(arg_item, num):
67
+ if isinstance(arg_item, Number):
69
68
  if not re.match(NUMBER_REGEX, str(arg_item.value)):
70
69
  return f'{class_name}("{arg_item.value}")'
71
70
  if flags.literal_shorthand:
72
71
  return str(arg_item.value)
73
72
  return f'{class_name}({arg_item.value})'
74
73
 
75
- if isinstance(arg_item, loc):
74
+ if isinstance(arg_item, Location):
76
75
  loc_components = [arg_item.x, arg_item.y, arg_item.z]
77
76
  if arg_item.pitch != 0:
78
77
  loc_components.append(arg_item.pitch)
@@ -80,28 +79,28 @@ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
80
79
  loc_components.append(arg_item.yaw)
81
80
  return f'{class_name}({", ".join(str(c) for c in loc_components)})'
82
81
 
83
- if isinstance(arg_item, var):
82
+ if isinstance(arg_item, Variable):
84
83
  if flags.var_shorthand:
85
- return f'"${VAR_SCOPES[arg_item.scope]}{arg_item.name}"'
84
+ return f'"${VAR_SCOPES[arg_item.scope]} {arg_item.name}"'
86
85
  if arg_item.scope == 'unsaved':
87
86
  return f'{class_name}("{arg_item.name}")'
88
87
  return f'{class_name}("{arg_item.name}", "{arg_item.scope}")'
89
88
 
90
- if isinstance(arg_item, sound):
89
+ if isinstance(arg_item, Sound):
91
90
  return f'{class_name}("{arg_item.name}", {arg_item.pitch}, {arg_item.vol})'
92
91
 
93
- if isinstance(arg_item, particle):
92
+ if isinstance(arg_item, Particle):
94
93
  return f'{class_name}({arg_item.particle_data})'
95
94
 
96
- if isinstance(arg_item, potion):
95
+ if isinstance(arg_item, Potion):
97
96
  return f'{class_name}("{arg_item.name}", {arg_item.dur}, {arg_item.amp})'
98
97
 
99
- if isinstance(arg_item, gamevalue):
98
+ if isinstance(arg_item, GameValue):
100
99
  if arg_item.target == 'Default':
101
100
  return f'{class_name}("{arg_item.name}")'
102
101
  return f'{class_name}("{arg_item.name}", "{arg_item.target}")'
103
102
 
104
- if isinstance(arg_item, parameter):
103
+ if isinstance(arg_item, Parameter):
105
104
  param_type_class_name = arg_item.param_type.__class__.__name__
106
105
  param_args = [f'"{arg_item.name}"', f'{param_type_class_name}.{arg_item.param_type.name}']
107
106
  if arg_item.plural:
@@ -116,7 +115,7 @@ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
116
115
  param_args.append(f'note="{arg_item.note}"')
117
116
  return f'{class_name}({", ".join(param_args)})'
118
117
 
119
- if isinstance(arg_item, vector):
118
+ if isinstance(arg_item, Vector):
120
119
  return f'{class_name}({arg_item.x}, {arg_item.y}, {arg_item.z})'
121
120
 
122
121
 
@@ -127,39 +126,62 @@ def add_script_line(flags: GeneratorFlags, script_lines: list[str], indent_level
127
126
  script_lines.append(added_line)
128
127
 
129
128
 
130
- # TODO: add tag values if not default
131
129
  def generate_script(template, flags: GeneratorFlags) -> str:
132
130
  indent_level = 0
133
131
  script_lines = []
134
132
  for codeblock in template.codeblocks:
135
- if codeblock.name == 'bracket':
136
- if codeblock.data['direct'] == 'open':
137
- add_script_line(flags, script_lines, indent_level, 't.bracket(', False)
138
- indent_level += 1
139
- elif codeblock.data['direct'] == 'close':
133
+ # Handle closing brackets
134
+ if codeblock.type == 'bracket':
135
+ if codeblock.data['direct'] == 'close':
140
136
  indent_level -= 1
141
- add_script_line(flags, script_lines, indent_level, ')')
142
- continue
143
- if codeblock.name == 'else':
144
- add_script_line(flags, script_lines, indent_level, 't.else_()')
137
+ add_script_line(flags, script_lines, indent_level, '])')
145
138
  continue
146
139
 
147
- method_name = TEMPLATE_METHOD_LOOKUP[codeblock.data['block']]
148
- method_args = [f'"{codeblock.name}"']
149
- if codeblock.name == 'dynamic':
150
- method_args[0] = f'"{codeblock.data["data"]}"'
140
+ # Get codeblock function and start its arguments with the action
141
+ function_name = TEMPLATE_FUNCTION_LOOKUP[codeblock.type]
142
+ function_args = [f'"{codeblock.action_name}"']
143
+
144
+ # Set function or process name if necessary
145
+ if codeblock.action_name == 'dynamic':
146
+ function_args[0] = f'"{codeblock.data["data"]}"'
151
147
 
148
+ # Convert argument objects to valid Python strings
152
149
  codeblock_args = [argument_item_to_string(flags, i) for i in codeblock.args]
153
150
  if codeblock_args:
154
- method_args.extend(codeblock_args)
155
- if method_name in TARGET_CODEBLOCKS and codeblock.target.name != 'SELECTION':
156
- method_args.append(f'target=Target.{codeblock.target.name}')
151
+ function_args.extend(codeblock_args)
152
+
153
+ # Add target if necessary
154
+ if function_name in TARGET_CODEBLOCKS and codeblock.target.name != 'SELECTION':
155
+ function_args.append(f'target=Target.{codeblock.target.name}')
156
+
157
+ # Add tags
157
158
  if codeblock.tags:
158
- default_tags = get_default_tags(codeblock.data.get('block'), codeblock.name)
159
+ default_tags = get_default_tags(codeblock.data.get('block'), codeblock.action_name)
159
160
  written_tags = {t: o for t, o in codeblock.tags.items() if default_tags[t] != o}
160
161
  if written_tags:
161
- method_args.append(f'tags={str(written_tags)}')
162
+ function_args.append(f'tags={str(written_tags)}')
162
163
 
163
- line = f't.{method_name}({", ".join(method_args)})'
164
- add_script_line(flags, script_lines, indent_level, line)
164
+ # Add sub-action for repeat
165
+ if codeblock.data.get('subAction'):
166
+ function_args.append(f'sub_action="{codeblock.data["subAction"]}"')
167
+
168
+ # Add inversion for NOT
169
+ if codeblock.data.get('attribute') == 'NOT':
170
+ function_args.append('inverted=True')
171
+
172
+ if codeblock.type in CONTAINER_CODEBLOCKS:
173
+ if codeblock.type == 'else':
174
+ line = f'{function_name}(['
175
+ elif codeblock.type in {'event', 'entity_event'}:
176
+ line = f'{function_name}({", ".join(function_args)}, [' # omit `codeblocks=` when we don't need it
177
+ else:
178
+ line = f'{function_name}({", ".join(function_args)}, codeblocks=['
179
+ add_script_line(flags, script_lines, indent_level, line, False)
180
+ indent_level += 1
181
+ else:
182
+ line = f'{function_name}({", ".join(function_args)})'
183
+ add_script_line(flags, script_lines, indent_level, line)
184
+
185
+ indent_level -= 1
186
+ add_script_line(flags, script_lines, indent_level, '])') # add final closing brackets
165
187
  return SCRIPT_START + '\n'.join(script_lines)
dfpyre/util.py CHANGED
@@ -26,3 +26,14 @@ def df_encode(json_string: str) -> str:
26
26
 
27
27
  def df_decode(encoded_string: str) -> str:
28
28
  return gzip.decompress(base64.b64decode(encoded_string)).decode('utf-8')
29
+
30
+
31
+ def flatten(nested_list: list):
32
+ """
33
+ Flattens a list.
34
+ """
35
+ for item in nested_list:
36
+ if isinstance(item, list):
37
+ yield from flatten(item)
38
+ else:
39
+ yield item