Skip to content

Commit

Permalink
core settings refactoring, avoid writing json file
Browse files Browse the repository at this point in the history
  • Loading branch information
domlysz committed Nov 24, 2020
1 parent 52d49c6 commit b0fbf67
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 65 deletions.
14 changes: 6 additions & 8 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def run_with_except_hook(*args2, **kwargs2):
ssl._create_default_https_context = ssl._create_unverified_context

#from .core.checkdeps import HAS_GDAL, HAS_PYPROJ, HAS_PIL, HAS_IMGIO
from .core.settings import getSettings, setSettings
from .core.settings import settings

#Import all modules which contains classes that must be registed (classes derived from bpy.types.*)
from . import prefs
Expand Down Expand Up @@ -349,14 +349,12 @@ def register():

#Setup prefs
preferences = bpy.context.preferences.addons[__package__].preferences
#>>logger
#logger = logging.getLogger(__name__)
logger.setLevel(logging.getLevelName(preferences.logLevel)) #will affect all child logger
#>>core settings
cfg = getSettings()
cfg['proj_engine'] = preferences.projEngine
cfg['img_engine'] = preferences.imgEngine
setSettings(cfg)

#update core settings according to addon prefs
settings.proj_engine = preferences.projEngine
settings.img_engine = preferences.imgEngine


def unregister():

Expand Down
2 changes: 1 addition & 1 deletion core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
logging.basicConfig(level=logging.getLevelName('INFO'))

from .checkdeps import HAS_GDAL, HAS_PYPROJ, HAS_IMGIO, HAS_PIL
from .settings import getSettings, setSettings
from .settings import settings
from .errors import OverlapError

from .utils import XY, BBOX
Expand Down
4 changes: 2 additions & 2 deletions core/basemaps/mapservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
from ..proj.ellps import dd2meters, meters2dd
from ..proj.srs import SRS

from ..settings import getSetting
USER_AGENT = getSetting('user_agent')
from .. import settings
USER_AGENT = settings.user_agent

# Set mosaic backgroung image color, it will be the base color for area not covered
# by the map service (ie when requests return non valid data)
Expand Down
5 changes: 2 additions & 3 deletions core/georaster/npimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from ..maths.fillnodata import replace_nans #inpainting function (ie fill nodata)
from ..utils import XY as xy
from ..checkdeps import HAS_GDAL, HAS_PIL, HAS_IMGIO
from ..settings import getSettings
from .. import settings

if HAS_PIL:
from PIL import Image
Expand All @@ -47,8 +47,7 @@ class NpImage():

def _getIFACE(self):

prefs = getSettings()
engine = prefs['img_engine']
engine = settings.img_engine

if engine == 'AUTO':
if HAS_GDAL:
Expand Down
5 changes: 2 additions & 3 deletions core/proj/reproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from ..errors import ReprojError
from ..utils import BBOX
from ..checkdeps import HAS_GDAL, HAS_PYPROJ
from ..settings import getSettings
from .. import settings

if HAS_GDAL:
from osgeo import osr, gdal
Expand Down Expand Up @@ -199,8 +199,7 @@ def __init__(self, crs1, crs2):
return

#Get proj engine from module settings
prefs = getSettings()
self.iproj = prefs['proj_engine']
self.iproj = settings.proj_engine
if self.iproj not in ['AUTO', 'GDAL', 'PYPROJ', 'BUILTIN', 'EPSGIO']:
raise ReprojError('Wrong engine name')

Expand Down
4 changes: 2 additions & 2 deletions core/proj/srv.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
import json
from ..settings import getSetting

USER_AGENT = getSetting('user_agent')
from .. import settings
USER_AGENT = settings.user_agent

######################################
# EPSG.io
Expand Down
2 changes: 1 addition & 1 deletion core/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"proj_engine": "AUTO",
"img_engine": "AUTO",
"user_agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
}
}
70 changes: 37 additions & 33 deletions core/settings.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
# -*- coding:utf-8 -*-

import os
import json

from .checkdeps import HAS_GDAL, HAS_PYPROJ, HAS_IMGIO, HAS_PIL
#from .proj import EPSGIO #WARN this one causes circular import because proj.reproj is imported in proj.__init__ and it also import settings.py

cfgFile = os.path.dirname(os.path.abspath(__file__)) + '/settings.json'
#cfgFile = os.path.join(os.path.dirname(__file__), "settings.json")

def getSettings():
with open(cfgFile, 'r') as cfg:
prefs = json.load(cfg)
return prefs

def setSettings(prefs):
with open(cfgFile, 'w') as cfg:
json.dump(prefs, cfg, indent='\t')

def getSetting(k):
prefs = getSettings()
return prefs.get(k, None)

def getAvailableProjEngines():
engines = ['AUTO', 'BUILTIN']
Expand All @@ -42,18 +24,40 @@ def getAvailableImgEngines():
engines.append('PIL')
return engines

def setImgEngine(engine):
if engine not in getAvailableImgEngines():
raise IOError
else:
cfg = getSettings()
cfg['img_engine'] = engine
setSettings(cfg)

def setProjEngine(engine):
if engine not in getAvailableProjEngines():
raise IOError
else:
cfg = getSettings()
cfg['proj_engine'] = engine
setSettings(cfg)

class Settings():

def __init__(self, **kwargs):
self._proj_engine = kwargs['proj_engine']
self._img_engine = kwargs['img_engine']
self.user_agent = kwargs['user_agent']

@property
def proj_engine(self):
return self._proj_engine

@proj_engine.setter
def proj_engine(self, engine):
if engine not in getAvailableProjEngines():
raise IOError
else:
self._proj_engine = engine

@property
def img_engine(self):
return self._img_engine

@img_engine.setter
def img_engine(self, engine):
if engine not in getAvailableImgEngines():
raise IOError
else:
self._img_engine = engine


cfgFile = os.path.join(os.path.dirname(__file__), "settings.json")

with open(cfgFile, 'r') as cfg:
prefs = json.load(cfg)

settings = Settings(**prefs)
5 changes: 2 additions & 3 deletions operators/io_get_dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
from .utils import adjust3Dview, getBBOX, isTopView
from ..core.proj import SRS, reprojBbox

from ..core.settings import getSetting

USER_AGENT = getSetting('user_agent')
from ..core import settings
USER_AGENT = settings.user_agent

PKG, SUBPKG = __package__.split('.', maxsplit=1)

Expand Down
4 changes: 2 additions & 2 deletions operators/io_import_osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from .utils import adjust3Dview, getBBOX, DropToGround, isTopView

from ..core.proj import Reproj, reprojBbox, reprojPt, utm
from ..core.settings import getSetting
from ..core.utils import perf_clock

USER_AGENT = getSetting('user_agent')
from ..core import settings
USER_AGENT = settings.user_agent

PKG, SUBPKG = __package__.split('.', maxsplit=1)

Expand Down
4 changes: 2 additions & 2 deletions operators/view3d_mapviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
from ..core import HAS_GDAL, HAS_PIL, HAS_IMGIO
from ..core.proj import reprojPt, reprojBbox, dd2meters, meters2dd
from ..core.basemaps import GRIDS, SOURCES, MapService
from ..core.settings import getSetting

USER_AGENT = getSetting('user_agent')
from ..core import settings
USER_AGENT = settings.user_agent

#bgis imports
from ..geoscene import GeoScene, SK, georefManagerLayout
Expand Down
13 changes: 8 additions & 5 deletions prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from .core.proj.reproj import EPSGIO
from .core.proj.srs import SRS
from .core.checkdeps import HAS_GDAL, HAS_PYPROJ, HAS_PIL, HAS_IMGIO
from .core.settings import getSetting, getSettings, setSettings
#from .core.settings import getSetting, getSettings, setSettings
from .core import settings

PKG = __package__

Expand Down Expand Up @@ -121,9 +122,10 @@ def getProjEngineItems(self, context):
return items

def updateProjEngine(self, context):
prefs = getSettings()
'''prefs = getSettings()
prefs['proj_engine'] = self.projEngine
setSettings(prefs)
setSettings(prefs)'''
settings.proj_engine = self.projEngine

projEngine: EnumProperty(
name = "Projection engine",
Expand All @@ -146,9 +148,10 @@ def getImgEngineItems(self, context):
return items

def updateImgEngine(self, context):
prefs = getSettings()
'''prefs = getSettings()
prefs['img_engine'] = self.imgEngine
setSettings(prefs)
setSettings(prefs)'''
settings.img_engine = self.imgEngine

imgEngine: EnumProperty(
name = "Image processing engine",
Expand Down

0 comments on commit b0fbf67

Please sign in to comment.