88
99import datetime
1010import json
11+ from typing import Any , TextIO
1112
1213from django .core .serializers .json import DjangoJSONEncoder
14+ from fs .osfs import OSFS
1315from lxml import etree
16+ from lxml .etree import _Element as Element
1417from opaque_keys .edx .keys import CourseKey , UsageKey
15- from xblock .fields import Scope
18+ from opaque_keys .edx .locator import BlockUsageLocator
19+ from xblock .core import XBlock
20+ from xblock .fields import Field , Scope
21+ from xblock .runtime import Runtime
1622
1723# Assume all XML files are persisted as utf-8.
1824EDX_XML_PARSER = etree .XMLParser (dtd_validation = False , load_dtd = False , remove_blank_text = True , encoding = "utf-8" )
@@ -41,15 +47,15 @@ def default(self, o):
4147 return super ().default (o )
4248
4349
44- def name_to_pathname (name ) :
50+ def name_to_pathname (name : str ) -> str :
4551 """
4652 Convert a location name for use in a path: replace ':' with '/'.
4753 This allows users of the xml format to organize content into directories
4854 """
4955 return name .replace (":" , "/" )
5056
5157
52- def is_pointer_tag (xml_obj ) :
58+ def is_pointer_tag (xml_obj : Element ) -> bool :
5359 """
5460 Check if xml_obj is a pointer tag: <blah url_name="something" />.
5561 No children, one attribute named url_name, no text.
@@ -73,7 +79,7 @@ def is_pointer_tag(xml_obj):
7379 return len (xml_obj ) == 0 and actual_attr == expected_attr and not has_text
7480
7581
76- def serialize_field (value ) :
82+ def serialize_field (value : Any ) -> str :
7783 """
7884 Return a string version of the value (where value is the JSON-formatted, internally stored value).
7985
@@ -90,7 +96,7 @@ def serialize_field(value):
9096 return json .dumps (value , cls = EdxJSONEncoder )
9197
9298
93- def deserialize_field (field , value ) :
99+ def deserialize_field (field : Field , value : str ) -> Any :
94100 """
95101 Deserialize the string version to the value stored internally.
96102
@@ -122,15 +128,15 @@ def deserialize_field(field, value):
122128 return value
123129
124130
125- def own_metadata (block ) :
131+ def own_metadata (block : XBlock ) -> dict [ str , Any ] :
126132 """
127133 Return a JSON-friendly dictionary that contains only non-inherited field
128134 keys, mapped to their serialized values
129135 """
130136 return block .get_explicitly_set_fields_by_scope (Scope .settings )
131137
132138
133- def apply_pointer_attributes (node , block ) -> None :
139+ def apply_pointer_attributes (node : Element , block : XBlock ) -> None :
134140 """Apply required pointer attributes to the relevant node for a block.
135141
136142 Sets "url_name" for all blocks. For course blocks, additionally assigns
@@ -145,12 +151,12 @@ def apply_pointer_attributes(node, block) -> None:
145151 node .set ("course" , block .location .course )
146152
147153
148- def format_filepath (category , name ) :
154+ def format_filepath (category : str , name : str ) -> str :
149155 """Formats a path to an XML definition file."""
150156 return f"{ category } /{ name } .{ filename_extension } "
151157
152158
153- def file_to_xml (file_object ) :
159+ def file_to_xml (file_object : TextIO ) -> Element :
154160 """
155161 Used when this module wants to parse a file object to xml
156162 that will be converted to the definition.
@@ -160,7 +166,7 @@ def file_to_xml(file_object):
160166 return etree .parse (file_object , parser = EDX_XML_PARSER ).getroot ()
161167
162168
163- def load_file (filepath , fs , def_id ) :
169+ def load_file (filepath : str , fs : OSFS , def_id : BlockUsageLocator ) -> Element :
164170 """
165171 Open the specified file in fs, and call `file_to_xml` on it,
166172 returning the lxml object.
@@ -175,7 +181,7 @@ def load_file(filepath, fs, def_id):
175181 raise Exception (f"Unable to load file contents at path { filepath } for item { def_id } : { err } " ) from err
176182
177183
178- def load_definition_xml (node , runtime , def_id ) :
184+ def load_definition_xml (node : Element , runtime : Runtime , def_id : BlockUsageLocator ) -> tuple [ Element , str ] :
179185 """
180186 Loads definition_xml stored in a dedicated file
181187 """
0 commit comments