Skip to content

Commit

Permalink
const file previously not included in the git repo. That is interesti…
Browse files Browse the repository at this point in the history
…ng, as it is required in the code. Someone building from repo would have not received this file. Needed for use in the CI pipeline / tests.
  • Loading branch information
Sean-McCann-HG committed Apr 19, 2024
1 parent 33cec7b commit ec61189
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions QGIS-AIMS-Plugin/AIMSDataManager/Const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
################################################################################
#
# Copyright 2015 Crown copyright (c)
# Land Information New Zealand and the New Zealand Government.
# All rights reserved
#
# This program is released under the terms of the 3 clause BSD license. See the
# LICENSE file for more information.
#
################################################################################

# NOTE
# This is potentially quite a dangerous way to set constants since it overwrites sys.modules and deletes the builtins.
# To prevent this we have to hold a _ref to sys to keep it from being deleted

import sys
from AIMSDataManager.Config import ConfigReader

class const:
'''Const class that reads config stored values and presents them as constant values that can be imported'''

class ConstError(TypeError): pass

def __init__(self):
'''Initialises and sets sel attributes from configreader values in 'const' section'''
CR = ConfigReader()
for key in CR.configSectionMap('const'):
val = CR.configSectionMap('const')[key]
setattr(self, key.upper(), val)

def __setattr__(self,name,value):
'''Overriding setattr method catching overwrites
@param name: Attribute name
@param value: Attribute value
'''
if self.__dict__.get(name):
raise self.ConstError('Can\'t rebind const {}'.format(name))
self.__dict__[name]=value

#sys.modules overwritten here but _ref holding builtins
_ref, sys.modules[__name__] = sys.modules[__name__], const()

0 comments on commit ec61189

Please sign in to comment.