import basic.constants as B def getPurKeyword(inkey): """ each key is case-insensitve in lower case. A keyword can have a singular or plural form - so the plural-s at the end is optional. A keyword can be assigned as keyword by the beginning digit underscore - so it is optional too. A keyword in a csv-file can be characterized by the keyword delimited with the in-filed-delimiter colon. a sub-keyword can be delimited :param inkey: :return: """ keyPur = inkey.lower() if ":" in keyPur: keyPur = keyPur.split(":").pop() if "-" in keyPur: keyPur = keyPur.split("-").pop() if keyPur[0:1] == "_": keyPur = keyPur[1:] if keyPur[-1:] == "s": keyPur = keyPur[:-1] return keyPur def getPluralKeyword(inkey): return getPurKeyword(inkey)+"s" def getSingularKeyword(inkey): if inkey == "stories": return B.SUBJECT_STORY return getPurKeyword(inkey) def getPluralAttribut(inkey): return "_"+getPurKeyword(inkey)+"s" def getSingularAttribut(inkey): return "_"+getPurKeyword(inkey) def getExistKeyword(inkey, inDict): """ returns the existing keyword in the dictionary :param inkey: keyword in any form (singular/plural, as attribute or not) :param inDict: the existing dictionary :return: """ if inkey in inDict: return inkey if getSingularKeyword(inkey) in inDict: return getSingularKeyword(inkey) if getPluralKeyword(inkey) in inDict: return getPluralKeyword(inkey) if getSingularAttribut(inkey) in inDict: return getSingularAttribut(inkey) if getPluralAttribut(inkey) in inDict: return getPluralAttribut(inkey) return "" def isStrDict(invalue): if isinstance(invalue, dict): return True if invalue.count(":") == invalue.count(",") + 1: return True return False def getStrDict(invalue): """ returns a dict of the input-value. If it is a string like json (delimited for key-value-pairs by comma and delimted the key-values by colon) then it is transformed into a dictionary :param invalue: :return: """ if isinstance(invalue, dict): return invalue outDict = {} if isStrDict(invalue): keyvalues = invalue.split(",") for x in keyvalues: kv = x.split(":") outDict[kv[0].replace("\"", "")] = kv[1].replace("\"", "") return outDict def getValueStr(invalue): if isinstance(invalue, dict): keys = list(invalue.keys()) if len(keys) == 0: return "" if len(keys) >= 1: return keys[0] if isinstance(invalue, list): if len(invalue) == 0: return "" if len(invalue) >= 1: return invalue[0] return invalue