def __init__(self, conn_dict, profile=None, echo=False): "Inititate connection" self._cred = struct(conn_dict) self._cred.kwargs = conn_dict.get('kwargs', {}) self.name = self._cred.get('name', None) self.username = self._cred.get('username', None) self.type = self._cred.type self.engine = None self._cursor_description = None self.profile = profile self.batch_size = 10000 self.fetch_size = 20000 self.echo = echo self.connect() self.last_connect = now() # Base Template template_base_path = '{}/database/templates/base.yaml'.format( get_dir_path()) self.template_dict = read_yaml(template_base_path) # Specific Type Template template_path = '{}/database/templates/{}.yaml'.format( get_dir_path(), self.type) temp_dict = read_yaml(template_path) for key1 in temp_dict: # Level 1 if isinstance(temp_dict[key1], dict): if key1 not in self.template_dict: self.template_dict[key1] = temp_dict[key1] # Level 2 for key2 in temp_dict[key1]: # Always Overwrite self.template_dict[key1][key2] = temp_dict[key1][key2] else: # Level 1 Non-Dict Overwrite self.template_dict[key1] = temp_dict[key1] self.variables = self._template('variables') if os.getenv('PROFILE_YAML'): other_vars = get_variables() for key in other_vars: self.variables[key] = other_vars[key] self.tmp_folder = self.variables['tmp_folder'] self.set_variables() if echo: log("Connected to {} as {}".format(self._cred.name, self._cred.user))
def get_jar_path(db_type, profile): from xutil.diskio import get_zip_path from zipfile import ZipFile from pathlib import Path import shutil tmp_folder = profile['variables']['tmp_folder'] jar_path = profile['drivers'][db_type]['path'] if Path(jar_path).exists(): return jar_path # try relative path jar_path = '{}/{}'.format(get_dir_path(__file__), profile['drivers'][db_type]['path']) if '.zip' in jar_path.lower(): new_jar_path = '{}/{}'.format(tmp_folder, Path(jar_path).name) if not Path(new_jar_path).exists(): zip_path = get_zip_path(jar_path) file_obj_path = jar_path.replace(zip_path + '/', '') with ZipFile(zip_path) as zip_f: with zip_f.open(file_obj_path) as zf, open(new_jar_path, 'wb') as f: shutil.copyfileobj(zf, f) jar_path = new_jar_path if not Path(jar_path).exists(): raise Exception('Could not find JAR path "{}"'.format(jar_path)) return jar_path
def test_file(self): test_data = '''hello\nthis comment\twith\n comma, 'tab' and "quotes"''' test_path = get_dir_path() + '/test.txt' # test write write_file(test_path, test_data) self.assertTrue(file_exists(test_path)) # test read data = read_file(test_path) self.assertEqual(data, test_data) # delete test file os.remove(test_path)
def test_yaml(self): # test write test_path = get_dir_path() + '/test.yaml' test_data = { 'name': 'Fritz', 'time': epoch(), 'address': { 'state': 'Florida' } } write_yaml(test_path, test_data) self.assertTrue(file_exists(test_path)) # test read data = read_yaml(test_path) self.assertEqual(data, test_data) # delete test file os.remove(test_path)
def test_csv(self): Row = namedtuple('Row', 'name state time comment') test_path = get_dir_path() + '/test.csv' test_data = [ Row('Fritz', 'Florida', str(epoch()), 'some\ncomment\nwith new line'), Row('James', 'California', str(epoch()), '''comment\twith\n comma, 'tab' and "quotes" ''') ] # test write write_csv(test_path, Row._fields, test_data) self.assertTrue(file_exists(test_path)) # test read data = read_csv(test_path) self.assertEqual(data, test_data) # delete test file os.remove(test_path) # test write stream def stream(): for row in test_data: yield row write_csvs(test_path, stream()) self.assertTrue(file_exists(test_path)) # test read stream data = list(read_csvS(test_path)) self.assertEqual(data, list(test_data)) # test read dataframe df = read_csvD(test_path) self.assertEqual(list(df.columns), list(Row._fields)) self.assertEqual(len(df), len(test_data)) self.assertEqual(df['comment'].loc[1], test_data[1].comment) # delete test file os.remove(test_path)
def alias_cli(): "Install alias" from xutil.helpers import get_home_path, get_dir_path, get_script_path from xutil.diskio import read_file, write_file from shutil import copyfile ans = input("Install 'alias.sh' in home directory (Y to proceed)? ") if ans.lower() != 'y': return src_path = get_dir_path() + '/alias.sh' dst_path = get_home_path() + '/.xutil.alias.sh' bash_profile_path = get_home_path() + '/.bashrc' # log('src_path -> ' + src_path) # log('dst_path -> ' + dst_path) copyfile(src_path, dst_path) bash_prof_text = read_file(bash_profile_path) if not dst_path in bash_prof_text: bash_prof_text = '{}\n\n. {}\n'.format(bash_prof_text, dst_path) write_file(bash_profile_path, bash_prof_text) log('+Updated ' + bash_profile_path)
import os, sys, copy, requests, json, random, string from io import StringIO, BytesIO from xutil.web import WebApp, process_request from xutil.helpers import jdumps, jtrans, log, get_error_str, get_script_path, get_dir_path, get_home_path, load_profile, file_exists from xutil.diskio import read_file, write_file, read_csv from dbnet.store import store_func from flask import render_template import yaml, apprise DBNET_FOLDER = os.getenv('DBNET_FOLDER', default=get_home_path() + '/dbnet') CSV_FOLDER = DBNET_FOLDER + '/csv' AUTH_PATH = DBNET_FOLDER + '/.authorized' app = WebApp('dbnet', root_path=get_dir_path(__file__)) SID = None last_perf_data = {} get_authorized = lambda: read_file(AUTH_PATH).splitlines() if file_exists( AUTH_PATH) else [] add_authorized = lambda new_id: write_file( AUTH_PATH, new_id + '\n', append=True) app_password = os.getenv('DBNET_PASSWD', default=None) app_token = ''.join( random.SystemRandom().choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(16)) valid_SIDs = set() cookie_to_sid = {} sid_to_sid = {} @app.route('/logo.ico')