export database to obo file

    :param path_to_file: path to export file
    :param connection: connection string (optional)
    :return:
506920
def export_obo(path_to_file, connection=None):
    
    db = DbManager(connection)
    db.export_obo(path_to_export_file=path_to_file)
    db.session.close()
132253
static void export(String path, DbConn cnx) throws JqmXmlException
    {
        if (cnx == null)
        {
            throw new IllegalArgumentException("database connection cannot be null");
        }
        export(path, JobDef.select(cnx, "jd_select_all"), cnx);
    }
118372
def save_id_to_path_mapping(self):
    
    if not self.id_to_path_mapping:
      return
    with open(self.local_id_to_path_mapping_file, 'w') as f:
      writer = csv.writer(f)
      writer.writerow(['id', 'path'])
      for k, v in sorted(iteritems(self.id_to_path_mapping)):
        writer.writerow([k, v])
    cmd = ['gsutil', 'cp', self.local_id_to_path_mapping_file,
           os.path.join(self.target_dir, 'id_to_path_mapping.csv')]
    if subprocess.call(cmd) != 0:
      logging.error('Can\'t copy id_to_path_mapping.csv to target directory')
444053
def persist(self, path_to_file):
        
        with open(path_to_file, 'wb') as f:
            f.write(self.data)
1004574
def _process_file(self, obj, fobj, field):
        
        from uliweb import settings
        
        paths = []
        upload_to = self.upload_to or self._get_upload_path(field, 'upload_to', obj)
        if upload_to:
            self.fileserving.to_path = upload_to
        upload_to_sub = self.upload_to_sub or self._get_upload_path(field, 'upload_to_sub', obj)
        if upload_to_sub:
            paths.append(upload_to_sub)
        paths.append(fobj['filename'])
        
        return self.fileserving.save_file(os.path.join(*paths), 
            fobj['file'], replace=self.file_replace, 
            convert=self.file_convert)
453247
def _connect(dbfile: 'PathLike') -> apsw.Connection:
    """"""
    conn = apsw.Connection(os.fspath(dbfile))
    _set_foreign_keys(conn, 1)
    assert _get_foreign_keys(conn) == 1
    return conn
813395
def load_txt_to_sql(tbl_name, src_file_and_path, src_file, op_folder):
    
    if op_folder == '':
        pth = ''
    else:
        pth = op_folder + os.sep
    
    fname_create_script = pth + 'CREATE_' + tbl_name + '.SQL'
    fname_backout_file  = pth + 'BACKOUT_' + tbl_name + '.SQL'
    fname_control_file  = pth + tbl_name + '.CTL'
   
    cols = read_csv_cols_to_table_cols(src_file)
    create_script_staging_table(fname_create_script, tbl_name, cols)    
    create_file(fname_backout_file, 'DROP TABLE ' + tbl_name + ' CASCADE CONSTRAINTS;\n')
    create_CTL(fname_control_file, tbl_name, cols, 'TRUNCATE')
549123
def WriteToPath(obj, filepath):
  
  with io.open(filepath, mode="w", encoding="utf-8") as filedesc:
    WriteToFile(obj, filedesc)
1312843
def to_file(self, path):
        
        with open(os.path.expanduser(path), "w") as ofile:
            ofile.write(self.__repr__())
1313865
def write_dict_to_file(file_path, obj):
    
    lines = []
    for key, value in obj.items():
        lines.append(key + ':' + repr(value) + '\n')

    with open(file_path, 'w+') as file:
        file.writelines(lines)

    return None
344340
def export_db(self, dirpath):
        
        if self.db is not None:
            self.db.commit()
            
        import virtualchain_hooks
        
        db_path = os.path.join(dirpath, os.path.basename(self.get_db_path()))
        snapshots_path = os.path.join(dirpath, os.path.basename(virtualchain.get_snapshots_filename(virtualchain_hooks, self.working_dir)))
        atlas_path = os.path.join(dirpath, 'atlas.db')
        subdomain_path = os.path.join(dirpath, 'subdomains.db')
        subdomain_queue_path = os.path.join(dirpath, 'subdomains.db.queue')

        src_atlas_path = os.path.join(self.working_dir, 'atlas.db')
        src_subdomain_path = os.path.join(self.working_dir, 'subdomains.db')
        src_subdomain_queue_path = os.path.join(self.working_dir, 'subdomains.db.queue')
        
        virtualchain.sqlite3_backup(self.get_db_path(), db_path)
        virtualchain.sqlite3_backup(virtualchain.get_snapshots_filename(virtualchain_hooks, self.working_dir), snapshots_path)

        if os.path.exists(src_atlas_path):
            virtualchain.sqlite3_backup(src_atlas_path, atlas_path)

        if os.path.exists(src_subdomain_path):
            virtualchain.sqlite3_backup(src_subdomain_path, subdomain_path)

        if os.path.exists(src_subdomain_queue_path):
            virtualchain.sqlite3_backup(src_subdomain_queue_path, subdomain_queue_path)
611860
def run(connection):
    

    parser = argparse.ArgumentParser(description="""
    Process database dumps.

    Either download of upload a dump file to the objectstore.

    downloads the latest dump and uploads with envronment and date
    into given container destination
    """)

    parser.add_argument(
        'location',
        nargs=1,
        default=f'{DUMPFOLDER}/database.{ENV}.dump',
        help="Dump file location")

    parser.add_argument(
        'objectstore',
        nargs=1,
        default=f'{DUMPFOLDER}/database.{ENV}.dump',
        help="Dump file objectstore location")

    parser.add_argument(
        '--download-db',
        action='store_true',
        dest='download',
        default=False,
        help='Download db')

    parser.add_argument(
        '--upload-db',
        action='store_true',
        dest='upload',
        default=False,
        help='Upload db')

    parser.add_argument(
        '--container',
        action='store_true',
        dest='container',
        default=False,
        help='Upload db')

    parser.add_argument(
        '--days',
        type=int,
        nargs=1,
        dest='days',
        default=0,
        help='Days to keep database dumps')

    args = parser.parse_args()

    if args.days:
        LOG.debug('Cleanup old dumps')
        remove_old_dumps(
            connection, args.objectstore[0], args.days[0])
    elif args.download:
        download_database(
            connection, args.objectstore[0], args.location[0])
    elif args.upload:
        upload_database(
            connection, args.objectstore[0], args.location[0])
    else:
        parser.print_help()
1126166
def get_file(filename):
    ''''''
    destination = __DOWNLOAD_PARAMS['path']
    filepath = os.path.join(destination, filename)

    if not __is_current(filepath):

        if not os.path.exists(destination):
            os.makedirs(destination)

        url = 'ftp://ftp.ebi.ac.uk/pub/databases/chebi/' + \
            'Flat_file_tab_delimited/'
        urlretrieve(urlparse.urljoin(url, filename), filepath)
        urlcleanup()

    if filepath.endswith('.zip'):
        zfile = zipfile.ZipFile(filepath, 'r')
        filepath = os.path.join(destination, zfile.namelist()[0])
        zfile.extractall(destination)
    elif filepath.endswith('.gz'):
        unzipped_filepath = filepath[:-len('.gz')]

        if os.path.exists(unzipped_filepath) \
                and __is_current(unzipped_filepath):
            filepath = unzipped_filepath
        else:
            input_file = gzip.open(filepath, 'rb')
            filepath = os.path.join(destination, input_file.name[:-len('.gz')])
            output_file = open(filepath, 'wb')

            for line in input_file:
                output_file.write(line)

            input_file.close()
            output_file.close()

    return filepath
636213
def to_file(file_):
        
        from sevenbridges.models.file import File
        if not file_:
            raise SbgError('File is required!')
        elif isinstance(file_, File):
            return file_.id
        elif isinstance(file_, six.string_types):
            return file_
        else:
            raise SbgError('Invalid file parameter!')
1092624
def run_export_db(filename=None):
    
    if not filename:
        filename = settings.DB_DUMP_FILENAME
    with cd(settings.FAB_SETTING('SERVER_PROJECT_ROOT')):
        run_workon('fab export_db:remote=True,filename={}'.format(filename))
993750
def database_to_csv(self, path, orderby='type'):
        
        tableinfo = self.get_table_info()
        stats_master = database_to_json(self.dbcon_master, tableinfo)
        stats_partial = database_to_json(self.dbcon_part, tableinfo)
        with open(path, 'w') as f:
            csvfile = csv.writer(f)
            csvfile.writerow(['Name', 'Type', 'Value', 'Description'])
            rows = []
            for tablename, info in tableinfo.iteritems():
                # Attempt to get the latest stat (from partial), if it doesn't exist get it from master
                value = stats_partial.get(tablename, {}).get('data', ValueError) or stats_master.get(tablename, {}).get('data', ValueError)
                if value is ValueError:
                    # The trackable was registered but no table values are found
                    value = self[tablename].current_value
                    if value is NO_STATE:
                        value = 'No Initial State'

                rows.append([tablename, info['type'], value, info['description']])

            if orderby == 'type':
                rows.sort(key=lambda x: x[1]) # Sort by type
            elif orderby == 'name':
                rows.sort(key=lambda x: x[0])  # Sort by type
            csvfile.writerows(rows)
480759
def export_json_object(dict_obj, filename=None):
    
    try:
        if filename:
            try:
                with open(filename, 'w') as handle:
                    handle.write(json.dumps(dict_obj, indent=4, sort_keys=True))
                    logger.info(
                        '%s: Wrote %s to local filesystem location' %
                        (inspect.stack()[0][3], filename))
                handle.close()
            except TypeError as e:
                logger.warning(
                    '%s: object in dict not serializable: %s' %
                    (inspect.stack()[0][3], str(e)))
        else:
            json_str = json.dumps(dict_obj, indent=4, sort_keys=True)
            print(highlight(json_str, lexers.JsonLexer(), formatters.TerminalFormatter()))
            logger.info('%s: successful export to stdout' % inspect.stack()[0][3])
            return True
    except IOError as e:
        logger.critical(
            '%s: export_file_object: error writing to %s to filesystem. Error: %s' %
            (inspect.stack()[0][3], filename, str(e)))
        return False
    else:
        logger.info('export_file_object: successful export to %s' % filename)
        return True