<s> '''<STR_LIT>''' <EOL> import clr <EOL> import re <EOL> import utils <EOL> import log <EOL> clr . AddReference ( '<STR_LIT>' ) <EOL> from System . IO import Path <EOL> __failed_regex = None <EOL> def regex ( filename_s , regex_s ) : <EOL> '''<STR_LIT>''' <EOL> global __failed_regex <EOL> results = None <EOL> if regex_s != __failed_regex : <EOL> try : <EOL> match = re . match ( regex_s , filename_s ) <EOL> if match : <EOL> founddict = { x : match . group ( x ) for x in match . groupdict ( ) <EOL> if match . group ( x ) and match . group ( x ) . strip ( ) } <EOL> if "<STR_LIT>" in founddict : <EOL> results = ( match . group ( "<STR_LIT>" ) , <EOL> match . group ( "<STR_LIT>" ) if "<STR_LIT>" in founddict else "<STR_LIT>" , <EOL> match . group ( "<STR_LIT>" ) if "<STR_LIT>" in founddict else "<STR_LIT>" ) <EOL> except : <EOL> log . debug_exc ( "<STR_LIT>" ) <EOL> __failed_regex = regex_s <EOL> results = None <EOL> return results <EOL> def extract ( filename_s ) : <EOL> '''<STR_LIT>''' <EOL> name_s = Path . GetFileName ( filename_s . strip ( ) ) <EOL> last_period = name_s . rfind ( r"<STR_LIT:.>" ) <EOL> name_s = name_s if last_period <= <NUM_LIT:0> else name_s [ <NUM_LIT:0> : last_period ] <EOL> match = re . match ( r"<STR_LIT>" + <EOL> r"<STR_LIT>" + <EOL> r"<STR_LIT>" , name_s ) <EOL> if match : name_s = match . group ( <NUM_LIT:2> ) <EOL> match = re . match ( r"<STR_LIT>" + <EOL> r"<STR_LIT>" + <EOL> r"<STR_LIT>" , name_s ) <EOL> if match : <EOL> log . debug ( name_s ) <EOL> name_s = match . group ( <NUM_LIT:1> ) + "<STR_LIT:U+0020>" + match . group ( <NUM_LIT:2> ) <EOL> log . debug ( "<STR_LIT>" , name_s ) <EOL> try : <EOL> retval = __extract ( name_s ) <EOL> if retval [ <NUM_LIT:0> ] . strip ( ) == "<STR_LIT>" : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except : <EOL> log . debug_exc ( "<STR_LIT>" + name_s + "<STR_LIT>" ) <EOL> retval = name_s , "<STR_LIT>" , "<STR_LIT>" <EOL> return retval <EOL> def __extract ( name_s ) : <EOL> '''<STR_LIT>''' <EOL> s = name_s <EOL> volume_year_s = __extract_year ( s ) <EOL> def recurse_sub ( pattern , s ) : <EOL> while re . search ( pattern , s ) : <EOL> s = re . sub ( pattern , "<STR_LIT>" , s ) <EOL> return s <EOL> s = recurse_sub ( r"<STR_LIT>" , s ) <EOL> s = recurse_sub ( r"<STR_LIT>" , s ) <EOL> s = recurse_sub ( r"<STR_LIT>" , s ) <EOL> s = re . sub ( r"<STR_LIT:_>" , "<STR_LIT:U+0020>" , s ) <EOL> s = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , s ) <EOL> s = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , s ) <EOL> s = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , s ) <EOL> s = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , s ) <EOL> s = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , s ) <EOL> if "<STR_LIT:->" in s and "<STR_LIT:U+0020>" not in s : <EOL> s = re . sub ( r"<STR_LIT>" , "<STR_LIT:U+0020>" , s ) <EOL> matches = __extract_numbers ( s ) <EOL> pattern = r"<STR_LIT>" <EOL> if len ( matches ) > <NUM_LIT:1> and re . match ( pattern , s ) : <EOL> s = re . sub ( pattern , "<STR_LIT>" , s , <NUM_LIT:1> ) <EOL> matches = __extract_numbers ( s ) <EOL> if len ( matches ) > <NUM_LIT:0> : <EOL> issue_num_s = matches [ - <NUM_LIT:1> ] . group ( <NUM_LIT:2> ) <EOL> series_s = s [ : matches [ - <NUM_LIT:1> ] . start ( <NUM_LIT:0> ) ] + s [ matches [ - <NUM_LIT:1> ] . end ( <NUM_LIT:0> ) : ] <EOL> matches = re . match ( "<STR_LIT>" , issue_num_s ) <EOL> issue_num_s = matches . group ( <NUM_LIT:2> ) if matches else issue_num_s <EOL> if re . match ( "<STR_LIT>" , issue_num_s ) and utils . is_number ( issue_num_s ) : <EOL> issue_num_s = utils . sstr ( float ( issue_num_s ) if '<STR_LIT:.>' in issue_num_s else int ( issue_num_s ) ) <EOL> else : <EOL> issue_num_s = "<STR_LIT>" <EOL> series_s = s <EOL> series_s = re . sub ( r"<STR_LIT>" , "<STR_LIT:U+0020>" , series_s ) . strip ( "<STR_LIT>" ) <EOL> return [ series_s , issue_num_s , volume_year_s ] <EOL> def __extract_year ( s ) : <EOL> '''<STR_LIT>''' <EOL> retval = "<STR_LIT>" <EOL> results = [ x [ <NUM_LIT:1> ] for x in <EOL> re . findall ( r"<STR_LIT>" , s ) if __isYear ( x [ <NUM_LIT:1> ] ) ] <EOL> if len ( results ) == <NUM_LIT:1> : <EOL> retval = results [ <NUM_LIT:0> ] <EOL> else : <EOL> results = re . findall ( r"<STR_LIT>" , s ) <EOL> results += re . findall ( r"<STR_LIT>" , s ) <EOL> results += re . findall ( r"<STR_LIT>" , s ) <EOL> results = [ x . strip ( r"<STR_LIT>" ) . strip ( ) for x in results ] <EOL> results = [ re . sub ( r"<STR_LIT>" , r"<STR_LIT>" , x ) for x in results ] <EOL> results = [ x for x in results if __isYear ( x ) ] <EOL> retval = results [ - <NUM_LIT:1> ] if results else "<STR_LIT>" <EOL> return retval <EOL> def __extract_numbers ( s ) : <EOL> '''<STR_LIT>''' <EOL> matches = list ( re . finditer ( r"<STR_LIT>" , s ) ) <EOL> is2000AD = re . match ( r"<STR_LIT>" , s ) <EOL> isBeano = re . match ( r"<STR_LIT>" , s ) <EOL> if not is2000AD and not isBeano : <EOL> matches = [ x for x in matches if not __isYear ( x . group ( <NUM_LIT:2> ) ) or <EOL> ( x . start ( <NUM_LIT:2> ) > <NUM_LIT:0> and s [ x . start ( <NUM_LIT:2> ) - <NUM_LIT:1> ] == '<STR_LIT:#>' ) ] <EOL> return matches <EOL> def __isYear ( d ) : <EOL> '''<STR_LIT>''' <EOL> return re . match ( r"<STR_LIT>" , d ) and int ( d ) > <NUM_LIT> and int ( d ) < <NUM_LIT> </s>
