dfpyre 0.8.17__tar.gz → 0.8.19__tar.gz

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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dfpyre
3
- Version: 0.8.17
3
+ Version: 0.8.19
4
4
  Summary: A package for creating and modifying code templates for the DiamondFire Minecraft server.
5
5
  Home-page: https://github.com/Amp63/pyre
6
6
  License: MIT
@@ -97,7 +97,7 @@ def parse_actiondump() -> ActiondumpResult:
97
97
 
98
98
 
99
99
  def get_default_tags(codeblock_type: str|None, codeblock_action: str|None) -> dict[str, str]:
100
- if codeblock_type is None or codeblock_action is None:
100
+ if not codeblock_type or not codeblock_action:
101
101
  return {}
102
102
  return {t['name']: t['default'] for t in CODEBLOCK_DATA[codeblock_type][codeblock_action]['tags']}
103
103
 
@@ -49,12 +49,22 @@ def item_to_string(class_name: str, i: Item, slot_argument: str):
49
49
  stripped_id = i.get_id().replace('minecraft:', '')
50
50
  if set(i.nbt.keys()) == {'id', 'count'}:
51
51
  if i.get_count() == 1:
52
- return f'{class_name}("{stripped_id}"{slot_argument})'
53
- return f'{class_name}("{stripped_id}", {i.get_count()}{slot_argument})'
54
- return f'{class_name}.from_snbt("""{i.get_snbt()}""")'
52
+ return f"{class_name}('{stripped_id}'{slot_argument})"
53
+ return f"{class_name}('{stripped_id}', {i.get_count()}{slot_argument})"
54
+
55
+ snbt_string = i.get_snbt().replace('\\"', '\\\\"')
56
+ return f'{class_name}.from_snbt("""{snbt_string}""")'
57
+
58
+
59
+ def escape(s: str) -> str:
60
+ return s.replace('\n', '\\n').replace("\'", "\\'")
61
+
62
+
63
+ def str_literal(s: str) -> str:
64
+ return "'" + escape(s) + "'"
55
65
 
56
66
 
57
- def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
67
+ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
58
68
  class_name = arg_item.__class__.__name__
59
69
  has_slot = arg_item.slot is not None and flags.preserve_slots
60
70
  slot_argument = f', slot={arg_item.slot}' if has_slot else ''
@@ -63,18 +73,18 @@ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
63
73
  return item_to_string(class_name, arg_item, slot_argument)
64
74
 
65
75
  if isinstance(arg_item, String):
66
- value = arg_item.value.replace('\n', '\\n')
76
+ literal = str_literal(arg_item.value)
67
77
  if not has_slot and flags.literal_shorthand:
68
- return f'"{value}"'
69
- return f'{class_name}("{value}"{slot_argument})'
78
+ return literal
79
+ return f"{class_name}({literal}{slot_argument})"
70
80
 
71
81
  if isinstance(arg_item, Text):
72
- value = arg_item.value.replace('\n', '\\n')
73
- return f'{class_name}("{value}"{slot_argument})'
82
+ literal = str_literal(arg_item.value)
83
+ return f"{class_name}('{literal}'{slot_argument})"
74
84
 
75
85
  if isinstance(arg_item, Number):
76
86
  if not is_number(str(arg_item.value)): # Probably a math expression
77
- return f'{class_name}("{arg_item.value}"{slot_argument})'
87
+ return f"{class_name}({str_literal(arg_item.value)}{slot_argument})"
78
88
  if not has_slot and flags.literal_shorthand:
79
89
  return str(arg_item.value)
80
90
  return f'{class_name}({arg_item.value}{slot_argument})'
@@ -88,29 +98,31 @@ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
88
98
  return f'{class_name}({", ".join(str(c) for c in loc_components)}{slot_argument})'
89
99
 
90
100
  if isinstance(arg_item, Variable):
101
+ name = escape(arg_item.name)
91
102
  if not has_slot and flags.var_shorthand:
92
- return f'"${VAR_SCOPES[arg_item.scope]} {arg_item.name}"'
103
+ return f"'${VAR_SCOPES[arg_item.scope]} {name}'"
93
104
  if arg_item.scope == 'unsaved':
94
- return f'{class_name}("{arg_item.name}"{slot_argument})'
95
- return f'{class_name}("{arg_item.name}", "{arg_item.scope}"{slot_argument})'
105
+ return f"{class_name}('{name}'{slot_argument})"
106
+ return f"{class_name}('{name}', '{arg_item.scope}'{slot_argument})"
96
107
 
97
108
  if isinstance(arg_item, Sound):
98
- return f'{class_name}("{arg_item.name}", {arg_item.pitch}, {arg_item.vol}{slot_argument})'
109
+ return f"{class_name}({str_literal(arg_item.name)}, {arg_item.pitch}, {arg_item.vol}{slot_argument})"
99
110
 
100
111
  if isinstance(arg_item, Particle):
101
112
  return f'{class_name}({arg_item.particle_data})'
102
113
 
103
114
  if isinstance(arg_item, Potion):
104
- return f'{class_name}("{arg_item.name}", {arg_item.dur}, {arg_item.amp}{slot_argument})'
115
+ return f"{class_name}({str_literal(arg_item.name)}, {arg_item.dur}, {arg_item.amp}{slot_argument})"
105
116
 
106
117
  if isinstance(arg_item, GameValue):
118
+ name = str_literal(arg_item.name)
107
119
  if arg_item.target == 'Default':
108
- return f'{class_name}("{arg_item.name}"{slot_argument})'
109
- return f'{class_name}("{arg_item.name}", "{arg_item.target}"{slot_argument})'
120
+ return f"{class_name}({name}{slot_argument})"
121
+ return f"{class_name}({name}, '{arg_item.target}'{slot_argument})"
110
122
 
111
123
  if isinstance(arg_item, Parameter):
112
124
  param_type_class_name = arg_item.param_type.__class__.__name__
113
- param_args = [f'"{arg_item.name}"', f'{param_type_class_name}.{arg_item.param_type.name}']
125
+ param_args = [str_literal(arg_item.name), f'{param_type_class_name}.{arg_item.param_type.name}']
114
126
  if arg_item.plural:
115
127
  param_args.append('plural=True')
116
128
  if arg_item.optional:
@@ -118,9 +130,9 @@ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
118
130
  if arg_item.default_value is not None:
119
131
  param_args.append(f'default_value={argument_item_to_string(flags, arg_item.default_value)}')
120
132
  if arg_item.description:
121
- param_args.append(f'description="{arg_item.description}"')
133
+ param_args.append(f"description={str_literal(arg_item.description)}")
122
134
  if arg_item.note:
123
- param_args.append(f'note="{arg_item.note}"')
135
+ param_args.append(f"note={str_literal(arg_item.note)}")
124
136
  return f'{class_name}({", ".join(param_args)}{slot_argument})'
125
137
 
126
138
  if isinstance(arg_item, Vector):
@@ -151,7 +163,8 @@ def generate_script(template, flags: GeneratorFlags) -> str:
151
163
  script_lines.append(IMPORT_STATEMENT + '\n')
152
164
 
153
165
  def remove_comma_from_last_line():
154
- script_lines[-1] = script_lines[-1][:-1]
166
+ if script_lines[-1].endswith(','):
167
+ script_lines[-1] = script_lines[-1][:-1]
155
168
 
156
169
  def get_var_assignment_snippet() -> str:
157
170
  first_block_data = template.codeblocks[0].data
@@ -174,7 +187,7 @@ def generate_script(template, flags: GeneratorFlags) -> str:
174
187
 
175
188
  # Get codeblock function and start its arguments with the action
176
189
  function_name = TEMPLATE_FUNCTION_LOOKUP[codeblock.type]
177
- function_args = [f'"{codeblock.action_name}"']
190
+ function_args = [f"'{codeblock.action_name}'"]
178
191
 
179
192
  # Add variable assignment if necessary
180
193
  var_assignment_snippet = ''
@@ -184,7 +197,7 @@ def generate_script(template, flags: GeneratorFlags) -> str:
184
197
 
185
198
  # Set function or process name if necessary
186
199
  if codeblock.action_name == 'dynamic':
187
- function_args[0] = f'"{codeblock.data["data"]}"'
200
+ function_args[0] = str_literal(codeblock.data["data"])
188
201
 
189
202
  # Convert argument objects to valid Python strings
190
203
  codeblock_args = [argument_item_to_string(flags, i) for i in codeblock.args]
@@ -204,18 +217,18 @@ def generate_script(template, flags: GeneratorFlags) -> str:
204
217
 
205
218
  # Add sub-action for repeat
206
219
  if codeblock.data.get('subAction'):
207
- function_args.append(f'sub_action="{codeblock.data["subAction"]}"')
220
+ function_args.append(f"sub_action='{codeblock.data["subAction"]}'")
208
221
 
209
222
  # Add inversion for NOT
210
223
  if codeblock.data.get('attribute') == 'NOT':
211
224
  function_args.append('inverted=True')
212
225
 
213
- # Create and add the final line
226
+ # Create and add the line
214
227
  if codeblock.type in CONTAINER_CODEBLOCKS:
215
228
  if codeblock.type == 'else':
216
229
  line = f'{function_name}(['
217
230
  elif codeblock.type in {'event', 'entity_event'}:
218
- line = f'{function_name}({", ".join(function_args)}, [' # omit `codeblocks=` when we don't need it
231
+ line = f'{var_assignment_snippet}{function_name}({", ".join(function_args)}, [' # omit `codeblocks=` when we don't need it
219
232
  else:
220
233
  line = f'{var_assignment_snippet}{function_name}({", ".join(function_args)}, codeblocks=['
221
234
  add_script_line(flags, script_lines, indent_level, line, False)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "dfpyre"
3
- version = "0.8.17"
3
+ version = "0.8.19"
4
4
  description = "A package for creating and modifying code templates for the DiamondFire Minecraft server."
5
5
  authors = ["Amp"]
6
6
  readme = "README.md"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes