def __init__(self, server_url, remove_pass_file = True, new_pass_file = True, password_location = '~',log_file = 'molgenis.log', logging_level='ERROR', logfile_mode = 'w'):
     '''Initialize Python api to talk to Molgenis Rest API
     
     Args:
         server_url (string):         The url to the molgenis server (ex: https://molgenis39.target.rug.nl/)
         remove_pass_file (bool):     If True, remove the files containing the passwords after usage (def: True)
         new_pass_file (str):         If file with password was not removed after last run, but still want to use a new password this run, set to True. Otherwise uses same password as last run (def: False)
         password_location (string):  Folder where to put the password files in (def: ~)
         log_file (string):           Path to write logfile with debug info etc to (def: molgenis.log)
         logging_level (string):      The level of logging to use. See Python's `logging` manual for info on levels (def: DEBUG)
         logfile_mode (string):       Mode of writing to logfile, e.g. w for overwrite or a for append, see `logging` manual for more details (def: w)
     '''
     # because errors in the __init__ function will not go to __exit__, make sure to clean up after error
     try:
         # if no path is specified in the log_file name, it should be written in the same location where the script is called from,
         # not from the location molgenis is located
         if not os.sep in log_file:
             log_file = os.getcwd()+os.sep+log_file
         else:
             # if there is a path in log_file, make sure that the folder exists
             if not os.path.exists(os.path.dirname(log_file)):
                 raise OSError('Folder "'+str(os.path.dirname)+'" for writing the molgenis.log file does not exist, change log_file location')
         logging.basicConfig(filename = log_file, filemode = logfile_mode)
         logging.getLogger().addHandler(logging.StreamHandler())
         self.logger = logging.getLogger(__name__)
         self.logger.setLevel(level=getattr(logging, logging_level))
         self.time_start = timeit.default_timer()
         security.overwrite_passphrase_location(password_location)
         if new_pass_file:
             self.remove_pass_file = True
             security.remove_secrets_file()
         security.require_username('Username')
         security.require_password('Password')
         self.session = molgenis.Session(server_url)
         self.logger.debug('Trying to log in with data from '+str(security.PASSPHRASE_FILE) +' to: '+server_url+' with username: '******'*'*len(security.retrieve('Username'))+' password: '******'*'*len(security.retrieve('Password')))
         self.session.login(security.retrieve('Username'), security.retrieve('Password'))
         self.entity_meta_data = {}
         self.column_meta_data = {}
         self.added_rows = 0
         self.added_files = 0
         self.remove_pass_file = remove_pass_file
     except:
         self.remove_password_files()
         raise
Exemple #2
0
 def __init__(self,
              server_url,
              remove_pass_file=True,
              new_pass_file=True,
              password_location='~',
              log_file='molgenis.log',
              logging_level='ERROR',
              logfile_mode='w'):
     '''Initialize Python api to talk to Molgenis Rest API
     
     Args:
         server_url (string):         The url to the molgenis server (ex: https://molgenis39.target.rug.nl/)
         remove_pass_file (bool):     If True, remove the files containing the passwords after usage (def: True)
         new_pass_file (str):         If file with password was not removed after last run, but still want to use a new password this run, set to True. Otherwise uses same password as last run (def: False)
         password_location (string):  Folder where to put the password files in (def: ~)
         log_file (string):           Path to write logfile with debug info etc to (def: molgenis.log)
         logging_level (string):      The level of logging to use. See Python's `logging` manual for info on levels (def: DEBUG)
         logfile_mode (string):       Mode of writing to logfile, e.g. w for overwrite or a for append, see `logging` manual for more details (def: w)
     '''
     # because errors in the __init__ function will not go to __exit__, make sure to clean up after error
     try:
         # if no path is specified in the log_file name, it should be written in the same location where the script is called from,
         # not from the location molgenis is located
         if not os.sep in log_file:
             log_file = os.getcwd() + os.sep + log_file
         else:
             # if there is a path in log_file, make sure that the folder exists
             if not os.path.exists(os.path.dirname(log_file)):
                 raise OSError(
                     'Folder "' + str(os.path.dirname) +
                     '" for writing the molgenis.log file does not exist, change log_file location'
                 )
         logging.basicConfig(filename=log_file,
                             filemode=logfile_mode)
         logging.getLogger().addHandler(logging.StreamHandler())
         self.logger = logging.getLogger(__name__)
         self.logger.setLevel(level=getattr(logging, logging_level))
         self.time_start = timeit.default_timer()
         security.overwrite_passphrase_location(password_location)
         if new_pass_file:
             self.remove_pass_file = True
             security.remove_secrets_file()
         security.require_username('Username')
         security.require_password('Password')
         self.session = molgenis.Session(server_url)
         self.logger.debug('Trying to log in with data from ' +
                           str(security.PASSPHRASE_FILE) + ' to: ' +
                           server_url + ' with username: '******'*' *
                           len(security.retrieve('Username')) +
                           ' password: '******'*' * len(security.retrieve('Password')))
         self.session.login(security.retrieve('Username'),
                            security.retrieve('Password'))
         self.entity_meta_data = {}
         self.column_meta_data = {}
         self.added_rows = 0
         self.added_files = 0
         self.remove_pass_file = remove_pass_file
     except:
         self.remove_password_files()
         raise
 def remove_password_files(self):
     if self.remove_pass_file:
         security.remove_secrets_file()
Exemple #4
0
 def remove_password_files(self):
     if self.remove_pass_file:
         security.remove_secrets_file()