Skip to content

Commit

Permalink
Restructure config module, user can dynamic set configure values now
Browse files Browse the repository at this point in the history
Signed-off-by: xcgspring <[email protected]>
  • Loading branch information
xcgspring committed Aug 4, 2015
1 parent 74588ac commit e61ed4d
Show file tree
Hide file tree
Showing 26 changed files with 1,209 additions and 455 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/AXUI.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

854 changes: 854 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

203 changes: 111 additions & 92 deletions AXUI/XML/XML_config.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,116 @@

import os

config_section="XML"
default_configs={ "app_map_location": os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "example"),
"schema_location": os.path.join(os.path.dirname(os.path.abspath(__file__)), "schemas"),
"timeout": 5,
"screenshot_location": os.path.dirname(os.path.abspath(__file__)),
"screenshot_on_failure": "False",
}

AppMapLocation=default_configs["app_map_location"]
SchemaLocation=default_configs["schema_location"]
TimeOut=default_configs["timeout"]
ScreenshotLocation=default_configs["screenshot_location"]
ScreenshotOnFailure=default_configs["screenshot_on_failure"]

def config(configs=None):
'''call back function used by config module
set the global variables according to configuration
class Config(object):
'''
if configs is None:
configs = default_configs

global AppMapLocation
global SchemaLocation
global TimeOut
global ScreenshotLocation
global ScreenshotOnFailure
AppMapLocation=configs["app_map_location"]
SchemaLocation=configs["schema_location"]
TimeOut=configs["timeout"]
ScreenshotLocation=configs["screenshot_location"]
ScreenshotOnFailure=configs["screenshot_on_failure"]

#used by config module
__all__=["config_section", "default_configs", "config"]

def query_app_map_file(app_map_file):
'''search app_map_file in AppMapLocation, return abs path if found
Arguments:
app_map_file: app_map file name
Returns:
abs_app_map_file: abs path of the app_map file
configs for core module
'''
if os.path.isabs(app_map_file) and os.path.isfile(app_map_file):
return app_map_file
else:
basename = os.path.basename(app_map_file)
for root, dirs, files in os.walk(AppMapLocation):
for file_ in files:
if file_ == basename:
return os.path.join(root, basename)
raise ValueError("%s not found in %s" % (app_map_file, AppMapLocation))

def query_schema_file(schema_file):
'''search schema_file in SchemaLocation, return abs path if found
Arguments:
schema_file: schema file name
Returns:
abs_schema_file: abs path of the schema file
'''
if os.path.isabs(schema_file) and os.path.isfile(schema_file):
return schema_file
else:
basename = os.path.basename(schema_file)
for root, dirs, files in os.walk(SchemaLocation):
for file_ in files:
if file_ == basename:
return os.path.join(root, basename)
raise ValueError("%s not found in %s" % (schema_file, SchemaLocation))

def query_timeout():
'''query timeout from config
Return: timeout set in config
'''
return TimeOut

def query_screenshot_location():
'''query screenshot_location from config
Return: screenshot_location
'''
if not os.path.isdir(ScreenshotLocation):
os.makedirs(ScreenshotLocation)

return ScreenshotLocation

def query_screenshot_on_failure():
'''query screenshot_on_failure from config
'''
results = {"True":True, "False":False}

if ScreenshotOnFailure in results:
return results[ScreenshotOnFailure]
else:
#use default
return results[default_configs["screenshot_on_failure"]]
_app_map_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "example")
_schema_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), "schemas")
_time_out = 5
_screenshot_location = os.path.dirname(os.path.abspath(__file__))
_screenshot_on_failure = False

def __str__(self):
'''
some help str for this config
'''
pass

@property
def app_map_location(self):
return self._app_map_location

@app_map_location.setter
def app_map_location(self, input):
#check if input location exist
if not os.path.isdir(input):
raise ValueError("Expect a valid directory for app_map_location, get %s" % input)
self._app_map_location = input

@property
def schema_location(self):
return self._schema_location

@schema_location.setter
def schema_location(self, input):
#check if input location exist
if not os.path.isdir(input):
raise ValueError("Expect a valid directory for schema_location, get %s" % input)
self._schema_location = input

@property
def time_out(self):
return self._time_out

@time_out.setter
def time_out(self, input):
#check if input valid
try:
self._time_out = int(input)
except ValueError:
raise ValueError("Expect an int value for global time out, get %s" % input)

@property
def screenshot_location(self):
return self._screenshot_location

@screenshot_location.setter
def screenshot_location(self, input):
#check if input valid
if not os.path.isabs(input):
raise ValueError("Expect a path for screenshot_location, get %s" % input)
else:
if not os.path.isdir(input):
os.makedirs(input)
self._screenshot_location = input

@property
def screenshot_on_failure(self):
return self._screenshot_on_failure

@screenshot_on_failure.setter
def screenshot_on_failure(self, input):
accepts = {"TRUE": True, "FALSE": False, True: True, False: False}
if input in accepts:
self._screenshot_on_failure = accepts[input]
elif isinstance(input, str) and input.upper() in accepts:
self._screenshot_on_failure = accepts[input.upper()]
else:
raise ValueError("Expect a True/False for screenshot_on_failure, get %s" % input)

def query_app_map_file(self, app_map_file):
'''search app_map_file in AppMapLocation, return abs path if found
Arguments:
app_map_file: app_map file name
Returns:
abs_app_map_file: abs path of the app_map file
'''
if os.path.isabs(app_map_file) and os.path.isfile(app_map_file):
return app_map_file
else:
basename = os.path.basename(app_map_file)
for root, dirs, files in os.walk(self.app_map_location):
for file_ in files:
if file_ == basename:
return os.path.join(root, basename)
raise ValueError("%s not found in %s" % (app_map_file, self.app_map_location))

def query_schema_file(self, schema_file):
'''search schema_file in SchemaLocation, return abs path if found
Arguments:
schema_file: schema file name
Returns:
abs_schema_file: abs path of the schema file
'''
if os.path.isabs(schema_file) and os.path.isfile(schema_file):
return schema_file
else:
basename = os.path.basename(schema_file)
for root, dirs, files in os.walk(self.schema_location):
for file_ in files:
if file_ == basename:
return os.path.join(root, basename)
raise ValueError("%s not found in %s" % (schema_file, self.schema_location))

core_config = Config()
2 changes: 1 addition & 1 deletion AXUI/XML/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#used by config module
from XML_config import *
from XML_config import core_config
#app map interface
from app_map import AppMap
10 changes: 5 additions & 5 deletions AXUI/XML/app_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
from AXUI.parsing.gui_command_parsing import gui_command_lexer, gui_command_parser
from AXUI.parsing.cli_command_parsing import cli_command_lexer, cli_command_parser
import func
import XML_config
from XML_config import core_config
import element as element_module

def singleton(class_):
instances = {}
def getinstance(xml, **kwargs):
full_xml = XML_config.query_app_map_file(xml)
full_xml = core_config.query_app_map_file(xml)
if full_xml not in instances:
#uplevel_app_map_xmls is a global value in decorater
#will overwrite it if there is argument passed in
Expand Down Expand Up @@ -76,7 +76,7 @@ def verification(self):
return

from validate import check_app_map
check_app_map(XML_config.query_schema_file("AXUI_app_map.xsd"), self.app_map_xml)
check_app_map(core_config.query_schema_file("AXUI_app_map.xsd"), self.app_map_xml)

def _parse_variable_elements(self, root_element):
variable_root = root_element.find("AXUI:variables", namespaces={"AXUI":"AXUI"})
Expand All @@ -93,8 +93,8 @@ def _parse_include_elements(self, root_element):
name = include_element.attrib["name"]
path = include_element.attrib["path"]
#prevent recursive include
if XML_config.query_app_map_file(path) in self.uplevel_app_map_xmls:
raise ValueError("Recursive include for app map: %s" % XML_config.query_app_map_file(path))
if core_config.query_app_map_file(path) in self.uplevel_app_map_xmls:
raise ValueError("Recursive include for app map: %s" % core_config.query_app_map_file(path))
self.app_maps[name] = AppMap(path, uplevel_app_map_xmls=self.uplevel_app_map_xmls)

def _parse_func_elements(self, root_element):
Expand Down
8 changes: 4 additions & 4 deletions AXUI/XML/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from AXUI.logger import LOGGER
from AXUI.driver import get_driver, DriverException
import XML_config
from XML_config import core_config

class TimeOutError(Exception):
pass
Expand Down Expand Up @@ -140,9 +140,9 @@ def __init__(self):
self.name = ""
self.parent_string = ""
self.identifier_string = ""
self.timeout = XML_config.query_timeout()
self.screenshot_location = XML_config.query_screenshot_location()
self.screenshot_on_failure = XML_config.query_screenshot_on_failure()
self.timeout = core_config.timeout
self.screenshot_location = core_config.screenshot_location
self.screenshot_on_failure = core_config.screenshot_on_failure
self.children = {}
self.parent = None
self.start_func = None
Expand Down
27 changes: 3 additions & 24 deletions AXUI/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@

import os
import config
import logger
import driver
import XML
import image
from config import config


def Config(config_file=""):
'''config the system
Arguments:
config_file: self defined config file, if not valid, will use default config file
'''
try:
if not os.path.isfile(config_file):
raise ValueError()
except ValueError:
print("config_file not valid, use default config file")
current_dir = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(current_dir, "global.cfg")

config.config_self(config_file)
config.config(logger)
config.config(XML)
config.config(driver)
config.config(image)

def assertIsValid(element, msg="element not valid"):
'''check if element valid
Expand All @@ -37,4 +16,4 @@ def assertIsValid(element, msg="element not valid"):
#port image_compare here
from image import image_compare

__all__=["Config", "assertIsValid", "AppMap", "image_compare"]
__all__=["config", "assertIsValid", "AppMap", "image_compare"]
Loading

0 comments on commit e61ed4d

Please sign in to comment.