KLL Compiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

file.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. '''
  3. KLL File Container
  4. '''
  5. # Copyright (C) 2016 by Jacob Alexander
  6. #
  7. # This file is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This file is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this file. If not, see <http://www.gnu.org/licenses/>.
  19. ### Imports ###
  20. import os
  21. import common.context as context
  22. ### Decorators ###
  23. ## Print Decorator Variables
  24. ERROR = '\033[5;1;31mERROR\033[0m:'
  25. WARNING = '\033[5;1;33mWARNING\033[0m:'
  26. ### Classes ###
  27. class KLLFile:
  28. '''
  29. Container class for imported KLL files
  30. '''
  31. def __init__( self, path, file_context ):
  32. '''
  33. Initialize file container
  34. @param path: Path to filename, if relative, relative to the execution environment
  35. @param context: KLL Context object
  36. '''
  37. self.path = path
  38. self.context = file_context
  39. self.lines = []
  40. self.data = ""
  41. def __repr__( self ):
  42. context_str = type( self.context ).__name__
  43. # Show layer info if this is a PartialMap
  44. if isinstance( self.context, context.PartialMapContext ):
  45. context_str = "{0}({1})".format( context_str, self.context.layer )
  46. return "({0}, {1})".format( self.path, context_str )
  47. def check( self ):
  48. '''
  49. Make sure that the file exists at the initialized path
  50. '''
  51. exists = os.path.isfile( self.path )
  52. # Display error message, will exit later
  53. if not exists:
  54. print( "{0} {1} does not exist...".format( ERROR, self.path ) )
  55. return exists
  56. def read( self ):
  57. '''
  58. Read the contents of the file path into memory
  59. Reads both per line and complete copies
  60. '''
  61. try:
  62. # Read file into memory, removing newlines
  63. with open( self.path ) as f:
  64. self.data = f.read()
  65. self.lines = self.data.splitlines()
  66. except:
  67. print( "{0} Failed to read '{1}' into memory...".format( ERROR, self.path ) )
  68. return False
  69. return True