def bruteStr(valu): ''' Brute force the version out of a string. Args: valu (str): String to attempt to get version information for. Notes: This first attempts to parse strings using the it:semver normalization before attempting to extract version parts out of the string. Returns: int, dict: The system normalized version integer and a subs dictionary. ''' try: valu, subs = s_datamodel.tlib.getTypeNorm('it:semver', valu) return valu, subs except s_common.BadTypeValu: # Try doing version part extraction by noming through the string subs = s_version.parseVersionParts(valu) if subs is None: raise s_common.BadTypeValu( valu=valu, mesg='Unable to brute force version parts out of the string') if subs: valu = s_version.packVersion(subs.get('major'), subs.get('minor', 0), subs.get('patch', 0)) return valu, subs
def bruteVersion(valu): ''' Attempt to brute force a valu into a semantic version string and its components Args: valu: A string or integer to attempt to obtain a system normalized version valu and subs for. Returns: int, dict: The system normalized version integer and a subs dictionary. ''' if isinstance(valu, int): return s_datamodel.tlib.getTypeNorm('it:semver', valu) if isinstance(valu, str): return bruteStr(valu) else: raise s_common.BadTypeValu(valu=valu, mesg='Unable to brute force a valu', type=type(valu))
def parse(text, base=None, chop=False): ''' Parse a time string into an epoch millis value. ''' #TODO: use base to facilitate relative time offsets text = text.strip().lower() text = (''.join([c for c in text if c.isdigit()])) if chop: text = text[:17] # TODO: support relative time offsets here... tlen = len(text) if tlen == 4: dt = datetime.datetime.strptime(text, '%Y') elif tlen == 6: dt = datetime.datetime.strptime(text, '%Y%m') elif tlen == 8: dt = datetime.datetime.strptime(text, '%Y%m%d') elif tlen == 10: dt = datetime.datetime.strptime(text, '%Y%m%d%H') elif tlen == 12: dt = datetime.datetime.strptime(text, '%Y%m%d%H%M') elif tlen == 14: dt = datetime.datetime.strptime(text, '%Y%m%d%H%M%S') elif tlen in (15, 16, 17): dt = datetime.datetime.strptime(text, '%Y%m%d%H%M%S%f') else: raise s_common.BadTypeValu(mesg='Unknown time format') epoch = datetime.datetime(1970, 1, 1) return int((dt - epoch).total_seconds() * 1000)
def _raiseBadValu(self, valu, **info): raise s_common.BadTypeValu(name=self.name, valu=valu, **info)