dfpyre 0.8.13__tar.gz → 0.8.15__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.13
3
+ Version: 0.8.15
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
@@ -52,20 +52,22 @@ def parse_actiondump() -> ActiondumpResult:
52
52
  codeblock_data = {n: {} for n in CODEBLOCK_NAME_LOOKUP.values()}
53
53
  codeblock_data['else'] = {'tags': []}
54
54
 
55
-
56
55
  if not os.path.exists(ACTIONDUMP_PATH):
57
56
  warn('data.json not found -- Item tags and error checking will not work.')
58
57
  return {}, set()
59
58
 
60
59
  with open(ACTIONDUMP_PATH, 'r', encoding='utf-8') as f:
61
60
  actiondump = json.loads(f.read())
62
-
63
61
  for action_data in actiondump['actions']:
64
62
  action_tags = get_action_tags(action_data)
65
- parsed_action_data = {'tags': action_tags}
63
+ parsed_action_data = {'tags': action_tags, 'required_rank': 'None'}
66
64
  if dep_note := action_data['icon']['deprecatedNote']:
67
65
  parsed_action_data['deprecatedNote'] = ' '.join(dep_note)
68
66
 
67
+ required_rank = action_data['icon']['requiredRank']
68
+ if required_rank:
69
+ parsed_action_data['required_rank'] = required_rank
70
+
69
71
  codeblock_name = CODEBLOCK_NAME_LOOKUP[action_data['codeblockName']]
70
72
  codeblock_data[codeblock_name][action_data['name']] = parsed_action_data
71
73
  if aliases := action_data['aliases']:
@@ -339,19 +339,22 @@ class DFTemplate:
339
339
  return template_item.send_to_minecraft()
340
340
 
341
341
 
342
- def generate_script(self, output_path: str, indent_size: int=4, literal_shorthand: bool=True, var_shorthand: bool=False, preserve_slots: bool=False):
342
+ def generate_script(self, indent_size: int=4, literal_shorthand: bool=True, var_shorthand: bool=False,
343
+ preserve_slots: bool=False, assign_variable: bool=False, include_import: bool=True,
344
+ build_and_send: bool=False) -> str:
343
345
  """
344
346
  Generate an equivalent python script for this template.
345
-
346
- :param str output_path: The file path to write the script to.
347
+
347
348
  :param int indent_size: The multiple of spaces to add when indenting lines.
348
- :param bool literal_shorthand: If True, `text` and `num` items will be written as strings and ints respectively.
349
+ :param bool literal_shorthand: If True, `Text` and `Number` items will be written as strings and ints respectively.
349
350
  :param bool var_shorthand: If True, all variables will be written using variable shorthand.
350
351
  :param bool preserve_slots: If True, the positions of items within chests will be saved.
352
+ :param bool assign_variable: If True, the generated template will be assigned to a variable.
353
+ :param bool include_import: If True, the `dfpyre` import statement will be added.
354
+ :param bool build_and_send: If True, `.build_and_send()` will be added to the end of the generated template.
351
355
  """
352
- flags = GeneratorFlags(indent_size, literal_shorthand, var_shorthand, preserve_slots)
353
- with open(output_path, 'w', encoding='utf-8') as f:
354
- f.write(generate_script(self, flags))
356
+ flags = GeneratorFlags(indent_size, literal_shorthand, var_shorthand, preserve_slots, assign_variable, include_import, build_and_send)
357
+ return generate_script(self, flags)
355
358
 
356
359
 
357
360
  def _assemble_template(starting_block: CodeBlock, codeblocks: list[CodeBlock], author: str|None) -> DFTemplate:
@@ -4,7 +4,7 @@ from dfpyre.items import *
4
4
  from dfpyre.actiondump import get_default_tags
5
5
 
6
6
 
7
- SCRIPT_START = '''from dfpyre import *\n\n'''
7
+ IMPORT_STATEMENT = 'from dfpyre import *'
8
8
 
9
9
  TEMPLATE_FUNCTION_LOOKUP = {
10
10
  'event': 'player_event',
@@ -38,6 +38,9 @@ class GeneratorFlags:
38
38
  literal_shorthand: bool
39
39
  var_shorthand: bool
40
40
  preserve_slots: bool
41
+ assign_variable: bool
42
+ include_import: bool
43
+ build_and_send: bool
41
44
 
42
45
 
43
46
  def item_to_string(class_name: str, i: Item, slot_argument: str):
@@ -123,6 +126,14 @@ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
123
126
  return f'{class_name}({arg_item.x}, {arg_item.y}, {arg_item.z}{slot_argument})'
124
127
 
125
128
 
129
+ def string_to_python_name(string: str) -> str:
130
+ """Converts `string` into a valid python identifier."""
131
+ string = string.strip()
132
+ if string[0].isnumeric():
133
+ string = '_' + string
134
+ return ''.join(c if c.isalnum() else '_' for c in string)
135
+
136
+
126
137
  def add_script_line(flags: GeneratorFlags, script_lines: list[str], indent_level: int, line: str, add_comma: bool=True):
127
138
  added_line = ' '*flags.indent_size*indent_level + line
128
139
  if add_comma and indent_level > 0:
@@ -133,9 +144,23 @@ def add_script_line(flags: GeneratorFlags, script_lines: list[str], indent_level
133
144
  def generate_script(template, flags: GeneratorFlags) -> str:
134
145
  indent_level = 0
135
146
  script_lines = []
147
+ variable_assigned = False
136
148
 
149
+ if flags.include_import:
150
+ script_lines.append(IMPORT_STATEMENT + '\n')
151
+
137
152
  def remove_comma_from_last_line():
138
153
  script_lines[-1] = script_lines[-1][:-1]
154
+
155
+ def get_var_assignment_snippet() -> str:
156
+ first_block_data = template.codeblocks[0].data
157
+ if 'data' in first_block_data:
158
+ name = first_block_data['data']
159
+ var_name = name if name else 'unnamed_template'
160
+ else:
161
+ var_name = first_block_data['block'] + '_' + first_block_data['action']
162
+ return f'{string_to_python_name(var_name)} = '
163
+
139
164
 
140
165
  for codeblock in template.codeblocks:
141
166
  # Handle closing brackets
@@ -150,6 +175,12 @@ def generate_script(template, flags: GeneratorFlags) -> str:
150
175
  function_name = TEMPLATE_FUNCTION_LOOKUP[codeblock.type]
151
176
  function_args = [f'"{codeblock.action_name}"']
152
177
 
178
+ # Add variable assignment if necessary
179
+ var_assignment_snippet = ''
180
+ if flags.assign_variable and not variable_assigned:
181
+ var_assignment_snippet = get_var_assignment_snippet()
182
+ variable_assigned = True
183
+
153
184
  # Set function or process name if necessary
154
185
  if codeblock.action_name == 'dynamic':
155
186
  function_args[0] = f'"{codeblock.data["data"]}"'
@@ -178,13 +209,14 @@ def generate_script(template, flags: GeneratorFlags) -> str:
178
209
  if codeblock.data.get('attribute') == 'NOT':
179
210
  function_args.append('inverted=True')
180
211
 
212
+ # Create and add the final line
181
213
  if codeblock.type in CONTAINER_CODEBLOCKS:
182
214
  if codeblock.type == 'else':
183
215
  line = f'{function_name}(['
184
216
  elif codeblock.type in {'event', 'entity_event'}:
185
217
  line = f'{function_name}({", ".join(function_args)}, [' # omit `codeblocks=` when we don't need it
186
218
  else:
187
- line = f'{function_name}({", ".join(function_args)}, codeblocks=['
219
+ line = f'{var_assignment_snippet}{function_name}({", ".join(function_args)}, codeblocks=['
188
220
  add_script_line(flags, script_lines, indent_level, line, False)
189
221
  indent_level += 1
190
222
  else:
@@ -194,4 +226,9 @@ def generate_script(template, flags: GeneratorFlags) -> str:
194
226
  remove_comma_from_last_line()
195
227
  indent_level -= 1
196
228
  add_script_line(flags, script_lines, indent_level, '])') # add final closing brackets
197
- return SCRIPT_START + '\n'.join(script_lines)
229
+
230
+ # Add `.build_and_send()` if necessary
231
+ if flags.build_and_send:
232
+ script_lines[-1] += '.build_and_send()'
233
+
234
+ return '\n'.join(script_lines)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "dfpyre"
3
- version = "0.8.13"
3
+ version = "0.8.15"
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