o 7]h2@sPddlTddlmZmZmZddlmZGdddZddeeDZ d S) )*)delimited_list any_open_tag any_close_tag)datetimec@s$eZdZdZeeZ eeZ e e  d eZ e e d eedZ ed d eZ e ede e dZ ed d eeeed eB d Z eeed  d eZ ed d eZ eeBeBdZ ed d eZ e ee dZ ed dZ! ed dZ"e"de"d dZ#ee"de"ddee"de"d dZ$e$%dd d e! d!Z&e'e#e&Be$B d" d"Z( ed# d$Z) e*dCd&e+fd'd(Z,e*dDd&e+fd*d+Z-ed, d-Z. ed. d/Z/ ed0 d1Z0 e1e2BZ3e*d2e+d3ed4e4fd5d6Z5e'e6e7d7e8e e9d7d8ee:d9e;e8d7B d:Z?ed ZA e*ed?d ZB ed@ dAZCeZDeZEe,ZFe-ZGe5ZHeAZIeBZJdBS)Epyparsing_commona" Here are some common low-level expressions that may be useful in jump-starting parser development: - numeric forms (:class:`integers`, :class:`reals`, :class:`scientific notation`) - common :class:`programming identifiers` - network addresses (:class:`MAC`, :class:`IPv4`, :class:`IPv6`) - ISO8601 :class:`dates` and :class:`datetime` - :class:`UUID` - :class:`comma-separated list` - :class:`url` Parse actions: - :class:`convertToInteger` - :class:`convertToFloat` - :class:`convertToDate` - :class:`convertToDatetime` - :class:`stripHTMLTags` - :class:`upcaseTokens` - :class:`downcaseTokens` Example:: pyparsing_common.number.runTests(''' # any int or real number, returned as the appropriate type 100 -100 +100 3.14159 6.02e23 1e-12 ''') pyparsing_common.fnumber.runTests(''' # any int or real number, returned as float 100 -100 +100 3.14159 6.02e23 1e-12 ''') pyparsing_common.hex_integer.runTests(''' # hex numbers 100 FF ''') pyparsing_common.fraction.runTests(''' # fractions 1/2 -3/4 ''') pyparsing_common.mixed_integer.runTests(''' # mixed fractions 1 1/2 -3/4 1-3/4 ''') import uuid pyparsing_common.uuid.setParseAction(tokenMap(uuid.UUID)) pyparsing_common.uuid.runTests(''' # uuid 12345678-1234-5678-1234-567812345678 ''') prints:: # any int or real number, returned as the appropriate type 100 [100] -100 [-100] +100 [100] 3.14159 [3.14159] 6.02e23 [6.02e+23] 1e-12 [1e-12] # any int or real number, returned as float 100 [100.0] -100 [-100.0] +100 [100.0] 3.14159 [3.14159] 6.02e23 [6.02e+23] 1e-12 [1e-12] # hex numbers 100 [256] FF [255] # fractions 1/2 [0.5] -3/4 [-0.75] # mixed fractions 1 [1] 1/2 [0.5] -3/4 [-0.75] 1-3/4 [1.75] # uuid 12345678-1234-5678-1234-567812345678 [UUID('12345678-1234-5678-1234-567812345678')] integerz hex integerz[+-]?\d+zsigned integer/fractioncCs|d|dS)Nr)ttrr>/usr/lib/python3/dist-packages/pip/_vendor/pyparsing/common.pyszpyparsing_common.-z"fraction or mixed integer-fractionz[+-]?(?:\d+\.\d*|\.\d+)z real numberz@[+-]?(?:\d+(?:[eE][+-]?\d+)|(?:\d+\.\d*|\.\d+)(?:[eE][+-]?\d+)?)z$real number with scientific notationnumberz[+-]?\d+\.?\d*([eE][+-]?\d+)?fnumber identifierzK(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}z IPv4 addressz[0-9a-fA-F]{1,4} hex_integer:zfull IPv6 address)rz::zshort IPv6 addresscCstdd|DdkS)Ncss |] }tj|rdVqdS)rN)r _ipv6_partmatches).0rrrr sz,pyparsing_common...)sumtrrrrsz::ffff:zmixed IPv6 addressz IPv6 addressz:[0-9a-fA-F]{2}([:.-])[0-9a-fA-F]{2}(?:\1[0-9a-fA-F]{2}){4}z MAC address%Y-%m-%dfmtcfdd}|S)a Helper to create a parse action for converting parsed date string to Python datetime.date Params - - fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%d"``) Example:: date_expr = pyparsing_common.iso8601_date.copy() date_expr.setParseAction(pyparsing_common.convertToDate()) print(date_expr.parseString("1999-12-31")) prints:: [datetime.date(1999, 12, 31)] c s@z t|dWSty}zt||t|d}~wwNr)rstrptimedate ValueErrorParseExceptionstr)ssllrver#rrcvt_fns z0pyparsing_common.convert_to_date..cvt_fnrr#r/rr.rconvert_to_date z pyparsing_common.convert_to_date%Y-%m-%dT%H:%M:%S.%fcr$)aHelper to create a parse action for converting parsed datetime string to Python datetime.datetime Params - - fmt - format to be passed to datetime.strptime (default= ``"%Y-%m-%dT%H:%M:%S.%f"``) Example:: dt_expr = pyparsing_common.iso8601_datetime.copy() dt_expr.setParseAction(pyparsing_common.convertToDatetime()) print(dt_expr.parseString("1999-12-31T23:59:59.999")) prints:: [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)] c s<z t|dWSty}zt||t|d}~wwr%)rr&r(r)r*)slr!r-r.rrr/*s z4pyparsing_common.convert_to_datetime..cvt_fnrr0rr.rconvert_to_datetimer2z$pyparsing_common.convert_to_datetimez7(?P\d{4})(?:-(?P\d\d)(?:-(?P\d\d))?)?z ISO8601 datez(?P\d{4})-(?P\d\d)-(?P\d\d)[T ](?P\d\d):(?P\d\d)(:(?P\d\d(\.\d*)?)?)?(?PZ|[+-]\d\d:?\d\d)?zISO8601 datetimez2[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}UUIDr4r5tokenscCstj|dS)aParse action to remove HTML tags from web page HTML source Example:: # strip HTML links from normal text text = 'More info at the pyparsing wiki page' td, td_end = makeHTMLTags("TD") table_text = td + SkipTo(td_end).setParseAction(pyparsing_common.stripHTMLTags)("body") + td_end print(table_text.parseString(text).body) Prints:: More info at the pyparsing wiki page r)r_html_strippertransform_string)r4r5r8rrrstrip_html_tagsAsz pyparsing_common.strip_html_tags,) exclude_charsz commaItem)defaultzcomma separated listcC|SN)upperr rrrrdcCrArB)lowerr rrrrgrDa^(?:(?:(?Phttps?|ftp):)?\/\/)(?:(?P\S+(?::\S*)?)@)?(?P(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(:(?P\d{2,5}))?(?P\/[^?# ]*)?(\?(?P[^#]*))?(#(?P\S*))?$urlN)r")r3)K__name__ __module__ __qualname____doc__ token_mapintconvert_to_integerfloatconvert_to_floatWordnumsset_nameset_parse_actionr hexnumsrRegexsigned_integerr add_parse_actionOptsuppress mixed_integerrrealsci_realsetName streamlinerr identcharsidentbodycharsr ipv4_addressr_full_ipv6_address_short_ipv6_address add_condition_mixed_ipv6_addressCombine ipv6_address mac_address staticmethodr*r1r6 iso8601_dateiso8601_datetimeuuidrrr9 ParseResultsr; OneOrMoreLiteralLineEnd printablesWhite FollowedBy _commasepitemr quoted_stringcopycomma_separated_list upcase_tokensdowncase_tokensrFconvertToIntegerconvertToFloat convertToDateconvertToDatetime stripHTMLTags upcaseTokensdowncaseTokensrrrrrs      .2rcCsg|] }t|tr|qSr) isinstance ParserElement)rvrrr s  rN) corehelpersrrrrrvarsvalues_builtin_exprsrrrrs !