44from io import BytesIO
55import json # The metadata format.
66import re # To find the path aliases.
7- from typing import Any , Dict , List , IO , Optional
7+ from typing import Any , Dict , List , IO
88import xml .etree .ElementTree as ET # For writing XML manifest files.
99import zipfile
1010
1515from Charon .filetypes .GCodeFile import GCodeFile # Required for fallback G-Code header parsing.
1616
1717
18- ## A container file type that contains multiple 3D-printing related files that
19- # belong together.
18+ # A container file type that contains multiple 3D-printing related files that
19+ # belong together.
2020class OpenPackagingConvention (FileInterface ):
2121 # Some constants related to this format.
2222 _xml_header = ET .ProcessingInstruction ("xml" ,
@@ -29,7 +29,7 @@ class OpenPackagingConvention(FileInterface):
2929
3030 mime_type = "application/x-opc"
3131
32- ## Initialises the fields of this class.
32+ # Initialises the fields of this class.
3333 def __init__ (self ) -> None :
3434 self ._mode = None # type: Optional[OpenMode] # Whether we're in read or write mode.
3535 self ._stream = None # type: Optional[IO[bytes]] # The currently open stream.
@@ -214,7 +214,7 @@ def toByteArray(self, offset: int = 0, count: int = -1) -> bytes:
214214 self ._zipfile = zipfile .ZipFile (self ._stream , self ._mode .value , compression = zipfile .ZIP_DEFLATED , allowZip64 = True )
215215 return result
216216
217- ## Adds a new content type to the archive.
217+ # Adds a new content type to the archive.
218218 # \param extension The file extension of the type
219219 def addContentType (self , extension : str , mime_type : str ) -> None :
220220 if not self ._stream :
@@ -230,7 +230,7 @@ def addContentType(self, extension: str, mime_type: str) -> None:
230230
231231 ET .SubElement (self ._content_types_element , "Default" , Extension = extension , ContentType = mime_type )
232232
233- ## Adds a relation concerning a file type.
233+ # Adds a relation concerning a file type.
234234 # \param virtual_path The target file that the relation is about.
235235 # \param relation_type The type of the relation. Any reader of OPC should
236236 # be able to understand all types that are added via relations.
@@ -267,7 +267,7 @@ def addRelation(self, virtual_path: str, relation_type: str, origin: str = "") -
267267 # Create the element itself.
268268 ET .SubElement (self ._relations [origin ], "Relationship" , Target = virtual_path , Type = relation_type , Id = unique_name )
269269
270- ## Figures out if a resource exists in the archive.
270+ # Figures out if a resource exists in the archive.
271271 #
272272 # This will not match on metadata, only on normal resources.
273273 # \param virtual_path: The path to test for.
@@ -287,7 +287,7 @@ def _resourceExists(self, virtual_path: str) -> bool:
287287 return True
288288 return False
289289
290- ## Dereference the aliases for OPC files.
290+ # Dereference the aliases for OPC files.
291291 #
292292 # This also adds a slash in front of every virtual path if it has no slash
293293 # yet, to allow referencing virtual paths with or without the initial
@@ -306,7 +306,7 @@ def _processAliases(self, virtual_path: str) -> str:
306306
307307 return virtual_path
308308
309- ## Convert the resource name inside the zip to a virtual path as this
309+ # Convert the resource name inside the zip to a virtual path as this
310310 # library specifies it should be.
311311 # \param zip_name The name in the zip file according to zipfile module.
312312 # \return The virtual path of that resource.
@@ -315,7 +315,7 @@ def _zipNameToVirtualPath(self, zip_name: str) -> str:
315315 return "/" + zip_name
316316 return zip_name
317317
318- ## Resize an image to the specified dimensions.
318+ # Resize an image to the specified dimensions.
319319 #
320320 # For now you may assume that the input image is PNG formatted.
321321 # \param virtual_path The virtual path pointing to an image in the
@@ -342,9 +342,9 @@ def _resizeImage(self, virtual_path: str, width: int, height: int) -> IO[bytes]:
342342 # TODO: Try other image loaders.
343343 raise # Raise import error again if we find no other image loaders.
344344
345- #### Below follow some methods to read/write components of the archive. ####
345+ # Below follow some methods to read/write components of the archive.
346346
347- ## When loading a file, load the relations from the archive.
347+ # When loading a file, load the relations from the archive.
348348 #
349349 # If the relations are missing, empty elements are created.
350350 def _readRels (self ) -> None :
@@ -374,12 +374,12 @@ def _readRels(self) -> None:
374374 origin_filename = virtual_path [virtual_path .rfind ("/" ) + 1 :- len (
375375 ".rels" )] # Just the filename (no path) and without .rels extension.
376376 origin_directory = directory [
377- :- len ("/_rels" )] # The parent path. We already know it's in the _rels directory.
377+ :- len ("/_rels" )] # The parent path. We already know it's in the _rels directory.
378378 origin = (origin_directory + "/" if (origin_directory != "" ) else "" ) + origin_filename
379379
380380 self ._relations [origin ] = document
381381
382- ## At the end of writing a file, write the relations to the archive.
382+ # At the end of writing a file, write the relations to the archive.
383383 #
384384 # This should be written at the end of writing an archive, when all
385385 # relations are known.
@@ -403,7 +403,7 @@ def _writeRels(self) -> None:
403403 self ._indent (element )
404404 self ._zipfile .writestr (relations_file , ET .tostring (self ._xml_header ) + b"\n " + ET .tostring (element ))
405405
406- ## When loading a file, load the content types from the archive.
406+ # When loading a file, load the content types from the archive.
407407 #
408408 # If the content types are missing, an empty element is created.
409409 def _readContentTypes (self ) -> None :
@@ -428,7 +428,7 @@ def _readContentTypes(self) -> None:
428428 ET .SubElement (self ._content_types_element , "Default" , Extension = "rels" ,
429429 ContentType = "application/vnd.openxmlformats-package.relationships+xml" )
430430
431- ## At the end of writing a file, write the content types to the archive.
431+ # At the end of writing a file, write the content types to the archive.
432432 #
433433 # This should be written at the end of writing an archive, when all
434434 # content types are known.
@@ -440,7 +440,7 @@ def _writeContentTypes(self) -> None:
440440 self ._zipfile .writestr (self ._content_types_file ,
441441 ET .tostring (self ._xml_header ) + b"\n " + ET .tostring (self ._content_types_element ))
442442
443- ## When loading a file, read its metadata from the archive.
443+ # When loading a file, read its metadata from the archive.
444444 #
445445 # This depends on the relations! Read the relations first!
446446 def _readMetadata (self ) -> None :
@@ -452,7 +452,7 @@ def _readMetadata(self) -> None:
452452 if "Target" not in relationship .attrib or "Type" not in relationship .attrib : # These two are required, and we actually need them here. Better ignore this one.
453453 continue
454454 if relationship .attrib [
455- "Type" ] != self ._opc_metadata_relationship_type : # Not interested in this one. It's not metadata that we recognise.
455+ "Type" ] != self ._opc_metadata_relationship_type : # Not interested in this one. It's not metadata that we recognise.
456456 continue
457457 metadata_file = relationship .attrib ["Target" ]
458458 if metadata_file not in self ._zipfile .namelist (): # The metadata file is unknown to us.
@@ -477,7 +477,7 @@ def _readMetadata(self) -> None:
477477 header_data = GCodeFile .parseHeader (gcode_stream , prefix = "/3D/model.gcode/" )
478478 self ._metadata .update (header_data )
479479
480- ## Reads a single node of metadata from a JSON document (recursively).
480+ # Reads a single node of metadata from a JSON document (recursively).
481481 # \param element The node in the JSON document to read.
482482 # \param current_path The path towards the current document.
483483 def _readMetadataElement (self , element : Dict [str , Any ], current_path : str ) -> None :
@@ -487,7 +487,7 @@ def _readMetadataElement(self, element: Dict[str, Any], current_path: str) -> No
487487 else :
488488 self ._metadata [current_path + "/" + key ] = value
489489
490- ## At the end of writing a file, write the metadata to the archive.
490+ # At the end of writing a file, write the metadata to the archive.
491491 #
492492 # This should be written at the end of writing an archive, when all
493493 # metadata is known.
@@ -522,7 +522,7 @@ def _writeMetadata(self) -> None:
522522 except OPCError : # User may already have defined this content type himself.
523523 pass
524524
525- ## Writes one dictionary of metadata to a JSON file.
525+ # Writes one dictionary of metadata to a JSON file.
526526 # \param metadata The metadata dictionary to write.
527527 # \param file_name The virtual path of the JSON file to write to.
528528 def _writeMetadataToFile (self , metadata : Dict [str , Any ], file_name : str ) -> None :
@@ -556,12 +556,12 @@ def _writeMetadataToFile(self, metadata: Dict[str, Any], file_name: str) -> None
556556
557557 self ._zipfile .writestr (file_name , json .dumps (document , sort_keys = True , indent = 4 ))
558558
559- ## Helper method to write data directly into an aliased path.
559+ # Helper method to write data directly into an aliased path.
560560 def _writeToAlias (self , path_alias : str , package_filename : str , file_data : bytes ) -> None :
561561 stream = self .getStream ("{}/{}" .format (path_alias , package_filename ))
562562 stream .write (file_data )
563563
564- ## Helper method to ensure a relationship exists.
564+ # Helper method to ensure a relationship exists.
565565 # Creates the relationship if it does not exists, ignores an OPC error if it already does.
566566 def _ensureRelationExists (self , virtual_path : str , relation_type : str , origin : str ) -> None :
567567 try :
@@ -570,7 +570,7 @@ def _ensureRelationExists(self, virtual_path: str, relation_type: str, origin: s
570570 except OPCError :
571571 pass
572572
573- ## Helper function for pretty-printing XML because ETree is stupid.
573+ # Helper function for pretty-printing XML because ETree is stupid.
574574 #
575575 # Source: https://stackoverflow.com/questions/749796/pretty-printing-xml-in-python
576576 def _indent (self , elem : ET .Element , level : int = 0 ) -> None :
@@ -589,6 +589,6 @@ def _indent(self, elem: ET.Element, level: int = 0) -> None:
589589 elem .tail = i
590590
591591
592- ## Error to raise that something went wrong with reading/writing a OPC file.
592+ # Error to raise that something went wrong with reading/writing a OPC file.
593593class OPCError (Exception ):
594594 pass # This is just a marker class.
0 commit comments