<s> import os <EOL> import logging <EOL> import inspect <EOL> from gooddataclient . exceptions import DataSetNotFoundError <EOL> from gooddataclient import text <EOL> from gooddataclient . columns import Column , Date , Attribute , ConnectionPoint , Label , Reference , Fact , get_date_dt_column , get_time_tm_column , get_tm_time_id_column <EOL> from gooddataclient . text import to_identifier , to_title <EOL> from gooddataclient . archiver import CSV_DATA_FILENAME <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> class Dataset ( object ) : <EOL> DATASETS_URI = '<STR_LIT>' <EOL> def __init__ ( self , project ) : <EOL> self . project = project <EOL> self . connection = project . connection <EOL> class Meta : <EOL> column_order = None <EOL> schema_name = None <EOL> project_name = None <EOL> @ property <EOL> def schema_name ( self ) : <EOL> return self . Meta . schema_name or self . __class__ . __name__ <EOL> @ property <EOL> def project_name ( self ) : <EOL> return self . Meta . project_name <EOL> def get_class_members ( self ) : <EOL> members = inspect . getmembers ( self , lambda member : isinstance ( member , Column ) ) <EOL> if not self . Meta . column_order : <EOL> return members <EOL> members_ordered = [ ] <EOL> for col_name in self . Meta . column_order : <EOL> for name , column in members : <EOL> if name == col_name : <EOL> members_ordered . append ( ( name , column ) ) <EOL> return members_ordered <EOL> def get_columns ( self ) : <EOL> columns = [ ] <EOL> for name , column in self . get_class_members ( ) : <EOL> column . name = to_identifier ( name ) <EOL> column . schema_name = to_identifier ( self . schema_name ) <EOL> columns . append ( column ) <EOL> return columns <EOL> def get_datasets_metadata ( self ) : <EOL> return self . connection . request ( self . DATASETS_URI % self . project . id ) <EOL> def get_metadata ( self , name ) : <EOL> response = self . get_datasets_metadata ( ) <EOL> for dataset in response [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> if dataset [ '<STR_LIT>' ] [ '<STR_LIT:title>' ] == name : <EOL> return dataset <EOL> raise DataSetNotFoundError ( '<STR_LIT>' % name ) <EOL> def delete ( self , name ) : <EOL> dataset = self . get_metadata ( name ) <EOL> return self . connection . request ( dataset [ '<STR_LIT>' ] [ '<STR_LIT>' ] , method = '<STR_LIT>' ) <EOL> def data ( self ) : <EOL> raise NotImplementedError <EOL> def get_date_dimension ( self ) : <EOL> for column in self . get_columns ( ) : <EOL> if isinstance ( column , Date ) : <EOL> return column <EOL> def create ( self ) : <EOL> date_dimension = self . get_date_dimension ( ) <EOL> if date_dimension : <EOL> DateDimension ( self . project ) . create ( name = date_dimension . schemaReference , <EOL> include_time = date_dimension . datetime ) <EOL> self . project . execute_maql ( self . get_maql ( ) ) <EOL> def upload ( self ) : <EOL> try : <EOL> self . get_metadata ( self . schema_name ) <EOL> except DataSetNotFoundError : <EOL> self . create ( ) <EOL> dir_name = self . connection . webdav . upload ( self . data ( ) , self . get_sli_manifest ( ) ) <EOL> self . project . integrate_uploaded_data ( dir_name ) <EOL> self . connection . webdav . delete ( dir_name ) <EOL> def get_folders ( self ) : <EOL> attribute_folders , fact_folders = [ ] , [ ] <EOL> for column in self . get_columns ( ) : <EOL> if column . folder : <EOL> if isinstance ( column , ( Attribute , Label , ConnectionPoint , Reference , Date ) ) : <EOL> if ( column . folder , column . folder_title ) not in attribute_folders : <EOL> attribute_folders . append ( ( column . folder , column . folder_title ) ) <EOL> if isinstance ( column , ( Fact , Date ) ) : <EOL> if ( column . folder , column . folder_title ) not in fact_folders : <EOL> fact_folders . append ( ( column . folder , column . folder_title ) ) <EOL> return attribute_folders , fact_folders <EOL> def get_sli_manifest ( self ) : <EOL> '''<STR_LIT>''' <EOL> parts = [ ] <EOL> for column in self . get_columns ( ) : <EOL> if isinstance ( column , Date ) : <EOL> parts . append ( get_date_dt_column ( column , self . schema_name ) ) <EOL> if column . datetime : <EOL> parts . append ( get_time_tm_column ( column , self . schema_name ) ) <EOL> parts . append ( get_tm_time_id_column ( column , self . schema_name ) ) <EOL> parts . append ( column . get_sli_manifest_part ( ) ) <EOL> return { "<STR_LIT>" : { "<STR_LIT>" : parts , <EOL> "<STR_LIT:file>" : CSV_DATA_FILENAME , <EOL> "<STR_LIT>" : '<STR_LIT>' % to_identifier ( self . schema_name ) , <EOL> "<STR_LIT>" : { "<STR_LIT>" : '<STR_LIT:">' , <EOL> "<STR_LIT>" : '<STR_LIT:">' , <EOL> "<STR_LIT>" : "<STR_LIT:U+002C>" , <EOL> "<STR_LIT>" : "<STR_LIT:\n>" <EOL> } } } <EOL> def get_maql ( self ) : <EOL> maql = [ ] <EOL> maql . append ( """<STR_LIT>""" % ( to_identifier ( self . schema_name ) , to_title ( self . schema_name ) ) ) <EOL> attribute_folders , fact_folders = self . get_folders ( ) <EOL> if attribute_folders or fact_folders : <EOL> maql . append ( '<STR_LIT>' ) <EOL> for folder , folder_title in attribute_folders : <EOL> maql . append ( '<STR_LIT>' % ( folder , folder_title ) ) <EOL> maql . append ( '<STR_LIT>' ) <EOL> for folder , folder_title in fact_folders : <EOL> maql . append ( '<STR_LIT>' % ( folder , folder_title ) ) <EOL> maql . append ( '<STR_LIT>' ) <EOL> maql . append ( '<STR_LIT>' ) <EOL> column_list = self . get_columns ( ) <EOL> for column in column_list : <EOL> if isinstance ( column , ( Attribute , ConnectionPoint ) ) or ( isinstance ( column , Date ) and not column . schemaReference ) : <EOL> maql . append ( column . get_maql ( ) ) <EOL> maql . append ( '<STR_LIT>' ) <EOL> for column in column_list : <EOL> if isinstance ( column , Fact ) : <EOL> maql . append ( column . get_maql ( ) ) <EOL> maql . append ( '<STR_LIT>' ) <EOL> for column in column_list : <EOL> if isinstance ( column , Date ) and column . schemaReference : <EOL> maql . append ( column . get_maql ( ) ) <EOL> maql . append ( '<STR_LIT>' ) <EOL> for column in column_list : <EOL> if isinstance ( column , Reference ) : <EOL> maql . append ( column . get_maql ( ) ) <EOL> default_set = False <EOL> for column in column_list : <EOL> if isinstance ( column , Label ) : <EOL> maql . append ( column . get_maql ( ) ) <EOL> if not default_set : <EOL> maql . append ( column . get_maql_default ( ) ) <EOL> default_set = True <EOL> cp = False <EOL> for column in column_list : <EOL> if isinstance ( column , ConnectionPoint ) : <EOL> cp = True <EOL> maql . append ( column . get_original_label_maql ( ) ) <EOL> if not cp : <EOL> maql . append ( '<STR_LIT>' % ( to_identifier ( self . schema_name ) , to_identifier ( self . schema_name ) , <EOL> to_identifier ( self . schema_name ) , to_identifier ( self . schema_name ) , <EOL> to_title ( self . schema_name ) , to_identifier ( self . schema_name ) , <EOL> to_identifier ( self . schema_name ) ) ) <EOL> maql . append ( """<STR_LIT>""" % to_identifier ( self . schema_name ) ) <EOL> return '<STR_LIT:\n>' . join ( maql ) <EOL> class DateDimension ( object ) : <EOL> DATE_MAQL = '<STR_LIT>' <EOL> DATE_MAQL_ID = '<STR_LIT>' <EOL> def __init__ ( self , project ) : <EOL> self . project = project <EOL> self . connection = project . connection <EOL> def get_maql ( self , name = None , include_time = False ) : <EOL> '''<STR_LIT>''' <EOL> if not name : <EOL> return self . DATE_MAQL <EOL> maql = self . DATE_MAQL_ID % ( text . to_identifier ( name ) , name ) <EOL> if include_time : <EOL> file_path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> time_dimension = open ( file_path ) . read ( ) . replace ( '<STR_LIT>' , text . to_identifier ( name ) ) . replace ( '<STR_LIT>' , name ) <EOL> maql = '<STR_LIT>' . join ( ( maql , time_dimension ) ) <EOL> return maql <EOL> def create ( self , name = None , include_time = False ) : <EOL> self . project . execute_maql ( self . get_maql ( name , include_time ) ) <EOL> if include_time : <EOL> self . upload_time ( name ) <EOL> return self <EOL> def upload_time ( self , name ) : <EOL> data = open ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) . read ( ) <EOL> sli_manifest = open ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) . read ( ) . replace ( '<STR_LIT>' , text . to_identifier ( name ) ) . replace ( '<STR_LIT>' , name ) <EOL> dir_name = self . connection . webdav . upload ( data , sli_manifest ) <EOL> self . project . integrate_uploaded_data ( dir_name , wait_for_finish = True ) <EOL> self . connection . webdav . delete ( dir_name ) </s>
