awips2/edexOsgi/com.raytheon.uf.common.localization.python/utility/common_static/base/python/PythonOverrider.py
Matt Nash 5b62180a2a Issue #1870 add Python and XML Incremental Override, implemented for both Java
and Python

Change-Id: I293b6abcdffbd2cef35e9b986362113186583c55

Former-commit-id: f544fdd9e1 [formerly 288bd3b02c] [formerly 7943769110 [formerly 27f86d4c39ce8b8e708408511c820a5dd765e4cb]]
Former-commit-id: 7943769110
Former-commit-id: ae21592cbf
2013-05-02 13:39:55 -05:00

118 lines
4 KiB
Python

# #
# This software was developed and / or modified by Raytheon Company,
# pursuant to Contract DG133W-05-CQ-1067 with the US Government.
#
# U.S. EXPORT CONTROLLED TECHNICAL DATA
# This software product contains export-restricted data whose
# export/transfer/disclosure is restricted by U.S. law. Dissemination
# to non-U.S. persons whether in the United States or abroad requires
# an export license or other authorization.
#
# Contractor Name: Raytheon Company
# Contractor Address: 6825 Pine Street, Suite 340
# Mail Stop B8
# Omaha, NE 68106
# 402.291.0100
#
# See the AWIPS II Master Rights File ("Master Rights File.pdf") for
# further licensing information.
# #
#
# Overrides python modules with each other and returns a new module
#
#
#
# SOFTWARE HISTORY
#
# Date Ticket# Engineer Description
# ------------ ---------- ----------- --------------------------
# 03/12/13 mnash Initial Creation.
#
#
#
import os
import imp
import inspect
from PathManager import PathManager
import LocalizationUtil
def override(name, loctype):
"""
Takes a name (filename and localization path) and the localization type and finds the
file and overrides it, and returns the module
Args:
name : the name and path of the file in localization
loctype : a string representation of the localization type
Returns:
a module that has all the correct methods after being overridden
"""
pathManager = PathManager()
tieredFiles = pathManager.getTieredLocalizationFile(loctype, name)
levels = pathManager.getAvailableLevels()
lfiles = list()
for level in levels :
if tieredFiles.has_key(level) :
lfiles.append(tieredFiles[level].getPath())
themodule = _internalOverride(lfiles)
return themodule
def _internalOverride(files):
"""
Takes the files and overrides them
Args:
files : the files that are to be overridden
Returns:
a new module that contains all the necessary elements
"""
themodule = imp.new_module('tmpmodule')
# modules = list of all the modules
for module in files :
# load each module, temporarily
tmpmodule = LocalizationUtil.loadModule(module)
the_module = _combineMembers(tmpmodule, themodule)
return themodule
def _combineMembers(tocombine, finalmodule):
"""
Loops over the necessary parts of each module and decides how to combine them
Args:
tocombine : the module to combine in
finalmodule : the module that is being combined into
Returns:
a new python module that was created above
"""
# get the functions
members = inspect.getmembers(tocombine, inspect.isfunction)
for member in members :
finalmodule.__setattr__(member[0], member[1])
# get the classes
classes = inspect.getmembers(tocombine, inspect.isclass)
for clazz in classes:
finalmodule.__setattr__(clazz[0], clazz[1])
for attr in dir(tocombine):
if attr.startswith('__') == False or attr.startswith('_') == False:
if hasattr(finalmodule, attr):
if isinstance(attr, dict):
# simply update dicts with the new keys
getattr(finalmodule, attr).update(getattr(tocombine, attr))
if isinstance(attr, list):
listattr = getattr(tocombine, attr)
for i in listattr:
# override each element in the list if it exists
getattr(finalmodule, attr)[i] = listattr[i]
else :
finalmodule.__setattr__(attr, getattr(tocombine, attr))
else :
if inspect.ismethod(attr) == False and inspect.isbuiltin(attr) == False and inspect.isclass(attr) == False:
finalmodule.__setattr__(attr, getattr(tocombine, attr))
return finalmodule