Changed to_dictionary and from_dictionary methods

This commit is contained in:
Mario Melara 2016-03-02 12:08:17 -08:00
parent 25f20b1967
commit 47c8e1366f

View File

@ -58,14 +58,11 @@ class Target(object):
Targets will have compiler finding strategies Targets will have compiler finding strategies
""" """
def __init__(self, name, compiler_strategy, module_name=None): def __init__(self, name, module_name=None):
self.name = name # case of cray "ivybridge" but if it's x86_64 self.name = name # case of cray "ivybridge" but if it's x86_64
self.compiler_strategy = compiler_strategy
self.module_name = module_name # craype-ivybridge self.module_name = module_name # craype-ivybridge
# Sets only the platform name to avoid recursiveness # Sets only the platform name to avoid recursiveness
def set_platform(self, platform):
self.platform_name = platform.name
def _cmp_key(self): def _cmp_key(self):
return (self.name, self.module_name) return (self.name, self.module_name)
@ -74,10 +71,14 @@ def __repr__(self):
return self.__str__() return self.__str__()
def __str__(self): def __str__(self):
if self.platform_name:
return self.platform_name + '-' + self.name
return self.name return self.name
def to_dict(self):
d = {}
d['name'] = self.name
d['module_name'] = self.module_name
return d
@key_ordering @key_ordering
class Platform(object): class Platform(object):
@ -109,7 +110,6 @@ def add_target(self, name, target):
raise ValueError( raise ValueError(
"%s is a spack reserved alias " "%s is a spack reserved alias "
"and cannot be the name of a target" % name) "and cannot be the name of a target" % name)
target.set_platform(self)
self.targets[name] = target self.targets[name] = target
def target(self, name): def target(self, name):
@ -194,6 +194,14 @@ def _cmp_key(self):
return (self.name, self.version, self.compiler_strategy) return (self.name, self.version, self.compiler_strategy)
def to_dict(self):
d = {}
d['name'] = self.name
d['version'] = self.version
d['compiler_strategy'] = self.compiler_strategy
return d
#NOTE: Key error caused because Architecture has no comparison method #NOTE: Key error caused because Architecture has no comparison method
@key_ordering @key_ordering
class Arch(object): class Arch(object):
@ -214,42 +222,54 @@ def _cmp_key(self):
target = self.target.name if isinstance(self.target, Target) else self.target target = self.target.name if isinstance(self.target, Target) else self.target
return (platform, os, target) return (platform, os, target)
def to_dict(self):
def _helper_to_dict(arch_field_dict, arch_field_name, *args):
""" General method to turn each class in architecture into a
dictionary. Takes as argument the class dictionary, the field name
(platform, platform_os, target) and then any attribute args
"""
d = {} d = {}
d[arch_field_name] = {} platform = self.platform
for items in args: platform_os = self.platform_os
d[arch_field_name][items] = arch_field_dict[items] target = self.target
d['platform'] = self.platform.name
d['platform_os'] = self.platform_os.to_dict()
d['target'] = self.target.to_dict()
return d return d
def to_dict(arch): #def _helper_to_dict(arch_field_dict, arch_field_name, *args):
""" Convert the Arch tuple into a dictionary for yaml dumping. This # """ General method to turn each class in architecture into a
uses the _helper_to_dict method to create the dictionary from the # dictionary. Takes as argument the class dictionary, the field name
provided architecture field. Can assign the architecture # (platform, platform_os, target) and then any attribute args
field name (either platform, platform_os or target) and any # """
attributes that make up that architecture field, # d = {}
""" # d[arch_field_name] = {}
d = {} # for items in args:
# d[arch_field_name][items] = arch_field_dict[items]
# return d
#
platform = arch.platform.__dict__ #def to_dict(arch):
platform_os = arch.platform_os.__dict__ # """ Convert the Arch tuple into a dictionary for yaml dumping. This
target = arch.target.__dict__ # uses the _helper_to_dict method to create the dictionary from the
# provided architecture field. Can assign the architecture
platform_dict = _helper_to_dict(platform,'platform','name') # field name (either platform, platform_os or target) and any
os_dict = _helper_to_dict(platform_os, 'platform_os', 'name','version', # attributes that make up that architecture field,
'compiler_strategy') # """
target_dict = _helper_to_dict(target,'target', 'name', # d = {}
'module_name','platform_name') #
# platform = arch.platform.__dict__
d.update(platform_dict) # platform_os = arch.platform_os.__dict__
d.update(os_dict) # target = arch.target.__dict__
d.update(target_dict) #
# platform_dict = _helper_to_dict(platform,'platform','name')
return d # os_dict = _helper_to_dict(platform_os, 'platform_os', 'name','version',
# 'compiler_strategy')
# target_dict = _helper_to_dict(target,'target', 'name',
# 'module_name','platform_name')
#
# d.update(platform_dict)
# d.update(os_dict)
# d.update(target_dict)
#
# return d
#def _platform_from_dict(platform): #def _platform_from_dict(platform):
# """Creates all the platform class module names into a dictionary of # """Creates all the platform class module names into a dictionary of
@ -287,17 +307,19 @@ def arch_from_dict(d):
helper methods to recreate the arch tuple from the dictionary read from helper methods to recreate the arch tuple from the dictionary read from
a yaml file a yaml file
""" """
arch = Arch()
if d is None: if d is None:
return None return None
platform_dict = d['platform'] os_dict = d['platform_os']
platform_os_dict = d['platform_os']
target_dict = d['target'] target_dict = d['target']
platform = d['platform']
platform_os = _operating_system_from_dict(platform_os_dict, platform)
target = _target_from_dict(target_dict) target = _target_from_dict(target_dict)
platform_os = _operating_system_from_dict(os_dict, arch.platform)
arch.target =target
arch.platform_os = platform_os
return Arch(platform, platform_os, target) return arch
@memoized @memoized
def all_platforms(): def all_platforms():