Esempio n. 1
0
def writeyaml2json(_filename: str, _yamlfile: str, _indent: int = 2, _sort_keys: bool = True, _minify: bool = False) -> None:
    """Convert YAML to JSON and write the JSON file (overwrites exist JSON files)"""
    ensurefileexists(_yamlfile)
    _tmpyaml: dict = yamlload(_yamlfile)  # nosec
    _out: str = jdump(_tmpyaml, indent=0, separators=(r',', r':'), sort_keys=_sort_keys).replace('\n', r'') if _minify else jdump(_tmpyaml, indent=2, separators=(r', ', r': '), sort_keys=_sort_keys)
    with open(_filename, mode=r'wt', encoding=r'utf-8') as _file:
        _file.write(_out)
Esempio n. 2
0
 def load_from_file(self, filename):
     """create metadata from schema file"""
     schema_file = file(filename)
     schema_yaml = yamlload(schema_file)
     schema_file.close()
     metadata = self.create_from(schema_yaml)
     return metadata
Esempio n. 3
0
def readTasksMilestones(yamlfile):
    with open(yamlfile, "rt") as fid:
        yatasks = yamlload(fid)

    #check if a starting time is present (this will allow relative time indicators)
    if "tstart" in yatasks:
        tstart = datestr2num(yatasks["tstart"])
    else:
        tstart = None

    #put the tasks in an ordered dict
    tasks = []
    for ky, task in yatasks["Tasks"].items():
        slot = []
        for period in task["periods"]:
            slot.append((conv2date(period[0],
                                   tstart), conv2date(period[1], tstart)))
        if "longname" in task:
            name = task["longname"]
        else:
            name = ky
        tasks.append(Task(name=name, slots=slot, color=task["color"]))
    milestones = []
    for ky, milestone in yatasks["Milestones"].items():
        if "color" in milestone:
            color = milestone["color"]
        else:
            color = "black"
        if "linewidth" in milestone:
            linewidth = milestone["linewidth"]
        else:
            linewidth = 4

        if "fontsize" in milestone:
            fontsize = milestone["fontsize"]
        else:
            fontsize = 24

        if "longname" in milestone:
            name = milestone["longname"]
        else:
            name = ky
        if "textshift" in milestone:
            textshift = conv2date(milestone["textshift"])
        else:
            textshift = 0

        milestones.append(
            MileStone(name=name,
                      epoch=conv2date(milestone["epoch"], tstart),
                      color=color,
                      linewidth=linewidth,
                      fontsize=fontsize,
                      textshift=textshift))

    return tasks, milestones
Esempio n. 4
0
def yaml2dict(_yaml: str) -> dict:
    r"""Convert a YAML string to a Python dictionary

    >>> yaml2dict("'0':\n- Val1\n- Val2\n- Val3\n- Val4\n'1':\n- '1'\n- '2'\n- '3'\n- '4'\n'2':\n- '5'\n- '6'\n- '7'\n- '8'\n'3':\n- '9'\n- '10'\n- '11'\n- '12'\n'4':\n- '13'\n- '14'\n- '15'\n- '16'\n'5':\n- '17'\n- '18'\n- '19'\n- '20'\n'6':\n- '3.14'\n- '6.28'\n- '2.73'\n- '1.57'\n")
    {'0': ['Val1', 'Val2', 'Val3', 'Val4'], '1': ['1', '2', '3', '4'], '2': ['5', '6', '7', '8'], '3': ['9', '10', '11', '12'], '4': ['13', '14', '15', '16'], '5': ['17', '18', '19', '20'], '6': ['3.14', '6.28', '2.73', '1.57']}
    """
    _buf = StringIO(_yaml)
    _out: dict = yamlload(_buf)  # nosec
    _buf.close()
    return _out
Esempio n. 5
0
def openyamlfile(_filename: str, _encoding: str = r'utf-8') -> dict:
    """Open an YAML file given a pathname and return the object as a dict"""
    try:
        ensurefileexists(_filename)
        with codec_opener(_filename, mode=r'rb', encoding=_encoding, buffering=1) as _file:
            return yamlload(_file)  # nosec
    except (MarkedYAMLError, YAMLError):
        stderr.write('The YAML file is malformed!\n')
        raise SystemExit(1)
    except (LookupError, UnicodeError):
        stderr.write('Unable to determine and process data encoding!\n')
        raise SystemExit(1)
Esempio n. 6
0
def yaml2json(_yaml: str, _indent: int = 2, _sort_keys: bool = True, _minify: bool = False) -> str:
    r"""Convert a YAML string to a JSON string

    >>> yaml2json("'0':\n- Val1\n- Val2\n- Val3\n- Val4\n'1':\n- '1'\n- '2'\n- '3'\n- '4'\n'2':\n- '5'\n- '6'\n- '7'\n- '8'\n'3':\n- '9'\n- '10'\n- '11'\n- '12'\n'4':\n- '13'\n- '14'\n- '15'\n- '16'\n'5':\n- '17'\n- '18'\n- '19'\n- '20'\n'6':\n- '3.14'\n- '6.28'\n- '2.73'\n- '1.57'\n", _sort_keys=True, _minify=True)
    '{"0":["Val1","Val2","Val3","Val4"],"1":["1","2","3","4"],"2":["5","6","7","8"],"3":["9","10","11","12"],"4":["13","14","15","16"],"5":["17","18","19","20"],"6":["3.14","6.28","2.73","1.57"]}'
    >>> yaml2json("'0':\n- Val1\n- Val2\n- Val3\n- Val4\n'1':\n- '1'\n- '2'\n- '3'\n- '4'\n'2':\n- '5'\n- '6'\n- '7'\n- '8'\n'3':\n- '9'\n- '10'\n- '11'\n- '12'\n'4':\n- '13'\n- '14'\n- '15'\n- '16'\n'5':\n- '17'\n- '18'\n- '19'\n- '20'\n'6':\n- '3.14'\n- '6.28'\n- '2.73'\n- '1.57'\n", _sort_keys=True, _minify=False)
        '{\n  "0": [\n    "Val1", \n    "Val2", \n    "Val3", \n    "Val4"\n  ], \n  "1": [\n    "1", \n    "2", \n    "3", \n    "4"\n  ], \n  "2": [\n    "5", \n    "6", \n    "7", \n    "8"\n  ], \n  "3": [\n    "9", \n    "10", \n    "11", \n    "12"\n  ], \n  "4": [\n    "13", \n    "14", \n    "15", \n    "16"\n  ], \n  "5": [\n    "17", \n    "18", \n    "19", \n    "20"\n  ], \n  "6": [\n    "3.14", \n    "6.28", \n    "2.73", \n    "1.57"\n  ]\n}'
    """
    _buf = StringIO(_yaml)
    _tmpyaml: dict = yamlload(_buf)  # nosec
    _out: str = jdump(_tmpyaml, indent=0, separators=(r',', r':'), sort_keys=_sort_keys).replace('\n', r'') if _minify else jdump(_tmpyaml, indent=2, separators=(r', ', r': '), sort_keys=_sort_keys)
    _buf.close()
    return _out
Esempio n. 7
0
    def load_mapfile(self, filename):
        """load map file"""
        map_file = file(filename)
        map_yaml = yamlload(map_file)
#        print map_yaml
        _LOGGER.debug("loag map_yaml %s"% str(map_yaml))
        for key in map_yaml.keys():
            if map_yaml[key] is None:
                raise Error("table.column is None")
            else:
                names = map_yaml[key].split('.')
                if len(names) != 2:
                    raise Error("table.column format error")
                else: 
                    self.tbdict[key] = names[0].lower() 
                    self.coldict[key] = names[1]
        self.dict = map_yaml
def get_token(config, options, sess):
    filename = relative_to_wordpress(config, options['storage_directory'], 'token.txt')
    if path.exists(filename):
        with open(filename, "r") as f:
            return yamlload(f)

    request_token = sess.obtain_request_token()
    url = sess.build_authorize_url(request_token)
    print "url:", url
    print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."
    raw_input()

    token = sess.obtain_access_token(request_token)
    token = (token.key, token.secret)
    with open(filename, "w") as f:
        f.write(yamlsave(token))

    return token
Esempio n. 9
0
def read_yaml(path):
    """ Reads the content of a .yaml file returns it as dictionary.

    Parameters
    ----------
    path: :obj:`str`
        Path to the .yaml file to be read.

    Returns
    -------
    yamlcontent: :obj:`dict`
        Content of the read .yaml file in dictionary format. """
    if not os.path.isfile(path):
        raise ImportError(("YAML file could not be found at location "
                           "'{}'.").format(path))
    with open(path) as runcard:
        yamlcontent = yamlload(runcard, Loader=Loader)
    return yamlcontent
def get_token(config, options, sess):
    filename = relative_to_wordpress(config, options['storage_directory'],
                                     'token.txt')
    if path.exists(filename):
        with open(filename, "r") as f:
            return yamlload(f)

    request_token = sess.obtain_request_token()
    url = sess.build_authorize_url(request_token)
    print "url:", url
    print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."
    raw_input()

    token = sess.obtain_access_token(request_token)
    token = (token.key, token.secret)
    with open(filename, "w") as f:
        f.write(yamlsave(token))

    return token
Esempio n. 11
0
    def load_mapfile(self, filename):
        """load map file"""
        if filename == None:
            raise Error('argments can not be None')
        if not os.path.isfile(filename):
            raise Error('map file %s does not exists' % filename)
        map_file = file(filename)
        map_yaml = yamlload(map_file)
#        print map_yaml
        _LOGGER.debug("load map_yaml %s"% str(map_yaml))
        tempdict = {}
        for key in map_yaml.keys():
            if map_yaml[key] is None:
                raise Error("Entity has none attribute.")
            for tempdict in map_yaml[key]:
                if len(tempdict) != 1:
                    raise Error("Format error when specify attribute %s" \
                                % str(tempdict))
                attr = tempdict.keys()[0]
                if tempdict[attr] is None:
                    continue
#                    raise Error("Table.Column is missing for %s.%s" % (key, attr))
                names = tempdict[attr].split('.')
                full_keyword = '.'.join([key,attr])
                if len(names) != 2:
                    raise Error("table.column format error")
                else:
                    self.dict[full_keyword] = tempdict[attr]
                    #self.tbdict[key] = names[0].lower()
                    self.tbdict[full_keyword] = names[0].lower()
                    self.tbdict_sen[full_keyword] = names[0]
                    self.coldict[full_keyword] = names[1]
                if not self.dict.has_key(key):
                    self.dict[key] = tempdict[attr]
                    self.tbdict[key] = names[0].lower()
                    self.tbdict_sen[key] = names[0]
                    self.coldict[key] = names[1]
                    self.entdict[key] = tempdict[attr]
        self.mapfile = filename
Esempio n. 12
0
 def __load_data(self):
     self.__data = self.__data or yamlload(open('data.yaml', 'r'))
Esempio n. 13
0
def InsertYAML(d_base, file_name):
    d_new = yamlload(file(file_name).read(), Loader=Loader)
    #print d_new
    InsertDict(d_base, d_new)
Esempio n. 14
0
        else:
            print '  ' * level, '[', k, ']=', v


#Insert a new dictionary to the base dictionary
def InsertDict(d_base, d_new):
    for k_new, v_new in d_new.items():
        if k_new in d_base and (type(v_new) == dict
                                and type(d_base[k_new]) == dict):
            InsertDict(d_base[k_new], v_new)
        else:
            d_base[k_new] = v_new


#Load a YAML and insert the data into a dictionary
def InsertYAML(d_base, file_name):
    d_new = yamlload(file(file_name).read(), Loader=Loader)
    #print d_new
    InsertDict(d_base, d_new)


#d_new= yamlload(file('data/b50.yaml').read(), Loader=Loader)
#d_new= yamlload(file('/home/akihiko/ros_ws/pr2_lfd_trick/.memory.yaml').read(), Loader=Loader)

d_new = yamlload(
    file('/home/akihiko/ros_ws/pr2_lfd_trick/.database.yaml').read(),
    Loader=Loader)
#d_new= yamlload(file('/home/akihiko/ros_ws/pr2_lfd_trick/.database.yaml').read())

print d_new
Esempio n. 15
0
- - [0.7933871709550895, 0.400622565503602]
  - [0.7797817537409618, 0.38848870605843544]
  - [0.7673072314648841, 0.397205199321492]
  - [0.7511308453397392, 0.3979779098025485]
  - [0.7376221198183579, 0.37922874083594077]
  - [0.7446540323785098, 0.35934877352607947]
  - [0.7340114044463709, 0.3520281346631997]
  - [0.7558130252203242, 0.3517181218042855]
  - [0.7572466893209524, 0.35739169235725643]
  - [0.7665287053165687, 0.35679494087551483]
  - [0.7649755237869639, 0.3487915071202909]
  - [0.7850848844172214, 0.350934209324011]
  - [0.7913994356610744, 0.35870436660736843]
  - [0.7856179491724727, 0.38702731095087556]
- - [0.8226085638203559, 0.3732243197807999]
  - [0.7830974213134687, 0.3958078112288826]
  - [0.7566822533976851, 0.38606266879387474]
  - [0.7449263533274906, 0.35814464251987604]
  - [0.7595845592026859, 0.35657054449646564]
  - [0.7891953209025128, 0.33843074561009395]
  - [0.7966620416413606, 0.3715367243730506]
  - [0.8131265258877942, 0.37344658913185746]
  - [0.8213627034463683, 0.3686180860123746]
'''
    contours = yamlload(datatext, Loader=Loader)
    for contour in contours:
        print '[' + ','.join([
            '[' + ','.join(map(lambda f: '%0.3f' % f, p)) + ']'
            for p in contour
        ]) + '],'
Esempio n. 16
0
def LoadYAML(file_name):
  return yamlload(open(file_name).read(), Loader=YLoader)
Esempio n. 17
0
def LoadYAML(file_name):
    return yamlload(file(file_name).read(), Loader=Loader)
Esempio n. 18
0
def read_yaml(filename):
    with open(filename, 'r') as yamlfile:
        return yamlload(yamlfile)
Esempio n. 19
0
def load(stream, Loader=YamlLoader):
    return yamlload(stream=stream, Loader=Loader)
Esempio n. 20
0
def LoadYAML(file_name):
  return yamlload(open(file_name).read(), Loader=YLoader)
Esempio n. 21
0
#!/usr/bin/python
#\file    yaml_to_csv.py
#\brief   Convert YAML to a CVS.
#\author  Akihiko Yamaguchi, [email protected]
#\version 0.1
#\date    May.25, 2017
from yaml import load as yamlload
from yaml import dump as yamldump
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

file_name = '/tmp/log.yaml'
data = yamlload(open(file_name, 'r').read(), Loader=Loader)
key_to_file = lambda key: file_name + '.' + key


def Main():
    for key, values in data.iteritems():
        print 'Found:', key
        if not isinstance(values, list):
            print '  non-list value:', values
            continue
        fp = open(key_to_file(key), 'w')
        for (tm, v) in values:
            if isinstance(v, list):
                fp.write('%f %s\n' % (tm, ' '.join(map(str, v))))
            else:
                fp.write('%f %f\n' % (tm, v))
        fp.close()
Esempio n. 22
0
 def LoadFromFile(self,filename):
     schemaFile = file(filename)
     pg = yamlload(schemaFile)
     schemaFile.close()
     md = self.CreateFrom(pg)
     return md