<s> import json <EOL> import logging <EOL> import re <EOL> import urlparse <EOL> from datetime import datetime <EOL> from dateutil import tz <EOL> from dateutil import parser <EOL> from django . utils . formats import localize_input <EOL> from django . utils . translation import ugettext as _ <EOL> from desktop . lib . parameterization import find_variables <EOL> from liboozie . oozie_api import get_oozie , DEFAULT_USER <EOL> LOG = logging . getLogger ( __name__ ) <EOL> JSON_FIELDS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> BOOLEAN_FIELDS = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> NUMBER_FIELDS_OR_NULL = ( '<STR_LIT>' , ) <EOL> GMT_TIME_FORMAT = "<STR_LIT>" <EOL> UTC_TIME_FORMAT = "<STR_LIT>" <EOL> FREQUENCY_REGEX = r'<STR_LIT>' <EOL> def format_field_value ( field , value ) : <EOL> if field in JSON_FIELDS : <EOL> if not isinstance ( value , basestring ) : <EOL> return json . dumps ( value ) <EOL> if field in NUMBER_FIELDS_OR_NULL : <EOL> if not isinstance ( value , int ) and value is not None : <EOL> return int ( value ) <EOL> if field in BOOLEAN_FIELDS : <EOL> return str ( value ) . lower ( ) == '<STR_LIT:true>' <EOL> return value <EOL> def format_dict_field_values ( dictionary ) : <EOL> for key in dictionary : <EOL> dictionary [ key ] = format_field_value ( key , dictionary [ key ] ) <EOL> return dictionary <EOL> def model_to_dict ( model ) : <EOL> from django . db import models <EOL> dictionary = { } <EOL> for field in model . _meta . fields : <EOL> try : <EOL> attr = getattr ( model , field . name , None ) <EOL> if isinstance ( attr , models . Model ) : <EOL> dictionary [ field . name ] = attr . id <EOL> elif isinstance ( attr , datetime ) : <EOL> dictionary [ field . name ] = str ( attr ) <EOL> else : <EOL> dictionary [ field . name ] = attr <EOL> except Exception , e : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % { '<STR_LIT>' : field . name , '<STR_LIT>' : str ( e ) } ) <EOL> return dictionary <EOL> def sanitize_node_dict ( node_dict ) : <EOL> for field in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if field in node_dict : <EOL> del node_dict [ field ] <EOL> return node_dict <EOL> def workflow_to_dict ( workflow ) : <EOL> workflow_dict = model_to_dict ( workflow ) <EOL> node_list = [ node . get_full_node ( ) for node in workflow . node_list ] <EOL> nodes = [ model_to_dict ( node ) for node in node_list ] <EOL> for index , node in enumerate ( node_list ) : <EOL> nodes [ index ] [ '<STR_LIT>' ] = [ model_to_dict ( link ) for link in node . get_all_children_links ( ) ] <EOL> workflow_dict [ '<STR_LIT>' ] = nodes <EOL> return workflow_dict <EOL> def smart_path ( path , mapping , is_coordinator = False ) : <EOL> if not path . startswith ( '<STR_LIT:$>' ) and not path . startswith ( '<STR_LIT:/>' ) and not urlparse . urlsplit ( path ) . scheme : <EOL> path = '<STR_LIT>' % { <EOL> '<STR_LIT:username>' : '<STR_LIT>' if is_coordinator else '<STR_LIT>' , <EOL> '<STR_LIT:path>' : path <EOL> } <EOL> if path . startswith ( '<STR_LIT:$>' ) : <EOL> variables = find_variables ( path ) <EOL> for var in variables : <EOL> prefix = '<STR_LIT>' % var <EOL> if path . startswith ( prefix ) : <EOL> if var in mapping : <EOL> if not urlparse . urlsplit ( mapping [ var ] ) . scheme and not mapping [ var ] . startswith ( '<STR_LIT:$>' ) : <EOL> path = '<STR_LIT>' % { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:path>' : path } <EOL> else : <EOL> if not urlparse . urlsplit ( path ) . scheme : <EOL> path = '<STR_LIT>' % { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:path>' : path } <EOL> return path <EOL> def contains_symlink ( path , mapping ) : <EOL> vars = find_variables ( path ) <EOL> return any ( [ var in mapping and '<STR_LIT:#>' in mapping [ var ] for var in vars ] ) or '<STR_LIT:#>' in path <EOL> def utc_datetime_format ( utc_time ) : <EOL> return utc_time . strftime ( UTC_TIME_FORMAT ) <EOL> def oozie_to_django_datetime ( dt_string ) : <EOL> try : <EOL> return localize_input ( datetime . strptime ( dt_string , UTC_TIME_FORMAT ) ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> return localize_input ( datetime . strptime ( dt_string , GMT_TIME_FORMAT ) ) <EOL> except ValueError : <EOL> pass <EOL> return None <EOL> class InvalidFrequency ( Exception ) : <EOL> pass <EOL> def oozie_to_hue_frequency ( frequency_string ) : <EOL> """<STR_LIT>""" <EOL> matches = re . match ( FREQUENCY_REGEX , frequency_string ) <EOL> if matches : <EOL> return matches . group ( '<STR_LIT>' ) , matches . group ( '<STR_LIT>' ) <EOL> else : <EOL> raise InvalidFrequency ( _ ( '<STR_LIT>' ) % frequency_string ) <EOL> def convert_to_server_timezone ( date , local_tz = '<STR_LIT>' , server_tz = None , user = DEFAULT_USER ) : <EOL> api = get_oozie ( user ) <EOL> if server_tz is None : <EOL> oozie_conf = api . get_configuration ( ) <EOL> server_tz = oozie_conf . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> if date and date . startswith ( '<STR_LIT:$>' ) : <EOL> return date <EOL> if date . endswith ( '<STR_LIT>' ) : <EOL> date = date [ : - <NUM_LIT:1> ] <EOL> local_tz = '<STR_LIT>' <EOL> try : <EOL> date_local_tz = parser . parse ( date ) <EOL> date_local_tz = date_local_tz . replace ( tzinfo = tz . gettz ( local_tz ) ) <EOL> date_server_tz = date_local_tz . astimezone ( tz . gettz ( server_tz ) ) <EOL> date_server_tz = date_server_tz . strftime ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' == server_tz : <EOL> return date_server_tz + u'<STR_LIT>' <EOL> else : <EOL> return date_server_tz + u'<STR_LIT:+>' + re . split ( '<STR_LIT>' , server_tz ) [ <NUM_LIT:1> ] <EOL> except TypeError , ValueError : <EOL> LOG . error ( "<STR_LIT>" % date ) <EOL> return None </s>
