# -*- coding: utf-8 -*-
"""We use a uniform naming convention method. The name is defined by different kinds of objects. The name is a string
its syntax is defined in a yaml file located at ``~/.cloudmesh/name.yaml``
::
order:
- experiment
- group
- user
- kind
- counter
schema: '{experiment}-{group}-{user}-{kind}-{counter}'
experiment: exp
group: grp
user: gregor
kind: container
counter: 2
This file is automatically generated if it does not exists by a simple `Name` object that can include an ordered
number of dictionary keys such as
:Experiment: is an experiment that all cloud objects can be placed under.
:Group: A group formulates a number of objects that logically build an entity,
such as a number of virtual machines building a cluster
:User: A user name that may control the group
:Kind: A kind that identifies which kind of resource this is
The last is a counter which is always increased and written into this file in order to assure that the latest
value is safely included in it.
A typical use is
::
n = Name(experiment="exp",
group="grp",
user="gregor",
kind="vm",
counter=1)
n.incr()
counter = n.get()
Which will return
::
exp-grp-gregor-vm-1
"""
import os
from pprint import pprint
import yaml
from cloudmesh.common.dotdict import dotdict
from cloudmesh.common.util import path_expand
[docs]class Name(dotdict):
def __init__(self, order=None, **kwargs):
"""
Defines a name tag that sets the format of the name to the specified schema
:param schema:
"""
if order is None:
order = ["experiment", "group", "user", "kind", "counter"]
if "path" not in kwargs:
self.__dict__['path'] = path_expand("~/.cloudmesh/name.yaml")
prefix = os.path.dirname(self.__dict__['path'])
if not os.path.exists(prefix):
os.makedirs(prefix)
if os.path.exists(self.path):
with open(self.path, 'rb') as dbfile:
v = yaml.safe_load(dbfile) or dict()
for name in v:
self.__dict__[name] = v[name]
self.__dict__['order'] = order
self.__dict__['schema'] = "{" + "}-{".join(order) + "}"
for name in kwargs:
self.__dict__[name] = kwargs[name]
# self.data = kwargs
def flush(self):
string = yaml.dump(self.__dict__, default_flow_style=False)
bits = bytes(string, encoding='utf-8')
with open(self.path, 'wb') as dbfile:
dbfile.write(bits)
def incr(self):
self.__dict__["counter"] += 1
self.flush()
def reset(self):
self.__dict__["counter"] = 1
self.flush()
def __str__(self):
return str(self.__dict__["schema"].format(**self.__dict__))
def dict(self):
return self.__dict__
def id(self, **kwargs):
d = self.__dict__
pprint(d)
for e in kwargs:
d[e] = kwargs[e]
pprint(d)
return str(self.__dict__["schema"].format(**d))
# Make a unit test
# %% usage/test
# a={'experiment':'a',
# 'group':'b',
# 'user':'gregor',
# 'counter':0}
# name=Name(a)
#
# print(name.get(a))
# print(name.get())