def newepobject(self, key, objname, defaultvalues=True, **kwargs): """create a new epj object""" # TODO test for dup name # TODO Kwargs strategy for array - # delay implementation for now, throw exception # TODO exceptions for wrong field name # TODO exception for wrong field value type # TODO documentation in usage.rst # should self.epobjects be updated here objepschema = self.epschema.epschemaobjects[key] try: nobj = self.epj[key][objname] = EPMunch() except KeyError as e: self.epj[key] = EPMunch() nobj = self.epj[key][objname] = EPMunch() for fieldname in objepschema.fieldnames(): try: if defaultvalues: fieldfprop = objepschema.fieldproperty(fieldname) nobj[fieldname] = fieldfprop["default"] except KeyError as e: prop = objepschema.fieldproperty(fieldname) # print(fieldname, prop.keys()) if "type" in prop: if prop["type"] == "array": # pprint(prop['items']) pass pass for key1, val1 in kwargs.items(): nobj[key1] = val1 nobj["eppykey"] = key nobj["eppyname"] = objname nobj["eppy_objepschema"] = objepschema return nobj
def newidfobject(self, key, objname, defaultvalues=True, **kwargs): """create a new idf object""" # TODO test for dup name # TODO Kwargs strategy for array - delay implementation for now, throw exception # TODO exceptions for wrong field name # TODO exception for wrong field value type # TODO documentation in usage.rst objidd = self.idd.iddobjects[key] try: nobj = self.idf[key][objname] = EPMunch() except KeyError as e: self.idf[key] = EPMunch() nobj = self.idf[key][objname] = EPMunch() for fieldname in objidd.fieldnames(): try: if defaultvalues: nobj[fieldname] = objidd.fieldproperty( fieldname)['default'] except KeyError as e: prop = objidd.fieldproperty(fieldname) # print(fieldname, prop.keys()) if 'type' in prop: if prop['type'] == 'array': # pprint(prop['items']) pass pass for key1, val1 in kwargs.items(): nobj[key1] = val1 nobj['eppykey'] = key nobj['eppyname'] = objname nobj['eppy_objidd'] = objidd return nobj
def copyidfobject(self, key, objname, newname): """copy an idf object with a new name""" # don't use the function dict.items() since the json for array has field name `items` oldobj = self.idf[key][objname] newobj = EPMunch() self.idf[key][newname] = newobj for key1 in oldobj.keys(): if not key1.startswith('eppy'): val1 = oldobj[key1] if isinstance(val1, list): newobj[key1] = list() for item in val1: newobj[key1].append(item.copy()) else: newobj[key1] = val1 newobj['eppyname'] = newname newobj['eppykey'] = key newobj['eppy_objidd'] = oldobj['eppy_objidd'] return newobj
def readidfjson(fhandle): """read an json idf Parameters ---------- fhandle: io.String, io.TextIOWrapper can be a file open for read or a io.StringIO object Returns ------- eppy.EPMunch """ try: fhandle = open(fhandle, 'r') except TypeError as e: pass as_json = json.load(fhandle) as_munch = EPMunch.fromDict(as_json) addeppykeys(as_munch) return as_munch
def copyepobject(self, key, objname, newname): """copy an epj object with a new name""" # don't use the function dict.items() since the json for array has # field name `items` # should self.epobjects be updated here oldobj = self.epj[key][objname] newobj = EPMunch() self.epj[key][newname] = newobj for key1 in oldobj.keys(): if not key1.startswith("eppy"): val1 = oldobj[key1] if isinstance(val1, list): newobj[key1] = list() for item in val1: newobj[key1].append(item.copy()) else: newobj[key1] = val1 newobj["eppyname"] = newname newobj["eppykey"] = key newobj["eppy_objepschema"] = oldobj["eppy_objepschema"] return newobj