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