Skip to content

Commit

Permalink
Merge pull request #3 from inus/fb5-test
Browse files Browse the repository at this point in the history
Add argparse handling in case called from a module
  • Loading branch information
inus authored May 16, 2024
2 parents 48d9a56 + 192ffac commit 11cba5f
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 60 deletions.
26 changes: 2 additions & 24 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,9 @@ jobs:
run: |
sudo apt update > /dev/null
sudo apt-get -q update
sudo apt-get -q -y install tmux vim libncurses5
wget -q https://github.com/FirebirdSQL/firebird/releases/download/R2_5_8/FirebirdCS-2.5.8.27089-0.amd64.tar.gz
tar xzf FirebirdCS-2.5.8.27089-0.amd64.tar.gz
pushd FirebirdCS-2.5.8.27089-0.amd64
tar xzf buildroot.tar.gz
mv opt/firebird /opt
popd
echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/firebird/lib" >> "$GITHUB_ENV"
sudo apt-get -q -y install tmux vim libncurses5 fbclient2
python -m pip -q install -U pip virtualenv flake8 pytest
if [ -f requirements.txt ]; then pip -q install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Unzip sample db
run: |
./fb_export -h
- name: Export test json data
run: |
./fb_export -d test/employee.fdb -e -o test/json -b
- name: Run tests with pytest
run: |
pytest
- name: Setup lhotari ssh session on disaster
if: ${{ failure() }}
if: ${{ failure() || success() }}
uses: lhotari/action-upterm@v1
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ celerybeat.pid

# Environments
.env
.venv
.venv*
env/
venv/
ENV/
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"program": "src/fb_export.py", //"${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": [ "-d", "test/employee.fdb", // "-e", // "-c", // "-l", //"-m 10", // "-u", "SYSDBA", "-p", "masterkey",
"-o" , "export", // "-F", "json"
"args": [ "test/employee.fdb", // "-e", // "-c", // "-l", //"-m 10", // "-u", "SYSDBA", "-p", "masterkey",
// "-o" , "OutDir", // "-F", "json"
] ,
},],
//other launch setting
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Change Log

## [0.2.0] - 2024-05-16
### Added:
Make it an importable unit. Add CHANGELOG.md. Remove shell script fb_export and replace with
python script.

## [0.1.0] - 2024-05-10
### Initial upload

Working with Firebird 2.5 and also via Github Actions runner on ubuntu-latest.


2 changes: 0 additions & 2 deletions fb_export

This file was deleted.

22 changes: 13 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ requires = ["setuptools",]
build-backend = "setuptools.build_meta"

[project]
name = "firebird-export"
name = "fb_export"
authors = [
{name = "Inus Scheepers", email = "[email protected]"}
]
description = "Export Firebird database to CSV, JSON or other pandas format from dataframe"
version = "0.1.0"
dynamic = ["version"]
readme = "README.md"
requires-python = ">=3.9"
dependencies = [
Expand Down Expand Up @@ -49,15 +49,11 @@ classifiers = [
"Topic :: File Formats",
]

[project.scripts]
firebird-export = "fb_export"

[project.urls]
Homepage = "https://github.com/inus/firebird-export"
#Documentation = "https://readthedocs.org"
#Repository = "https://github.com/me/spam.git"
#Issues = "https://github.com/me/spam/issues"
#Changelog = "https://github.com/me/spam/blob/master/CHANGELOG.md"
Repository = "https://github.com/inus/firebird-export.git"
Issues = "https://github.com/inus/firebird-export/issues"
Changelog = "https://github.com/inus/firebird-export/CHANGELOG.md"

[tool.pytest.ini_options]
testpaths = "test"
Expand All @@ -70,3 +66,11 @@ log_date_format = "%Y-%m-%d %H:%M:%S"
#filterwarnings = "ignore"
#norecursedirs = docs build

[project.scripts]
fb_export = "fb_export.fb_export:main"

[tool.setuptools]
package-dir = {"fb_export" = "src"}

[tool.setuptools.dynamic]
version = {attr = "fb_export.about.VERSION"}
4 changes: 2 additions & 2 deletions src/about.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# about.py
VERSION = "0.0.1"
DATE = '2024-05-10'
VERSION = "0.2.0"
DATE = '2024-05-16'
32 changes: 18 additions & 14 deletions src/args.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
# Firebird export arguments
from about import VERSION

import argparse


def get_args():
def get_args(*fbe_args):

parser = argparse.ArgumentParser(description="Firebird export")

parser.add_argument( 'path_to_db', type=str,
help="Firebird server alias or database name \
(eg. employee) or path to database file, eg ./employee.fdb")

parser.add_argument('--version', action='version', version='%(prog)s version ' + VERSION)
parser.add_argument("-e", "--export", action='store_true', default=False, help="Export data")
parser.add_argument('-b', '--brief', action='store_true', default=False,)
parser.add_argument('-b', '--brief', action='store_true', default=False,help="Shorter info")
parser.add_argument('-l', '--limit', action='store_true', default=False,
help="Limit tables and fields to those in customselect.py")
parser.add_argument('-m', '--maxrows', help="Max number of rows returned in export")

#fixme - numsamples to be conditional on sampledata
help="Limit tables and fields to those in limit_sql.py")
parser.add_argument('-m', '--maxrows', help="Max number of rows to export")
parser.add_argument('-s', '--sampledata', action='store_true', default=False,
help="Also show sample record")
help="Also show sample data records")
parser.add_argument('-n', '--numsamples', default=3, help="number of sample rows")

parser.add_argument('-c', '--combine', action='store_true', default=False,
help="Combine output into a single file")
parser.add_argument('-j', '--join', action='store_true', default=False,
help="Join output files")
format = [ 'csv', 'json',] # 'excel', 'sql', 'hdf', 'pickle', 'html' ....]?
parser.add_argument("-F", "--format", choices=format, default='csv', help="Export output format, default .CSV")
parser.add_argument("-o", "--outdir", type=str, dest='outdir', default='Export', help="Output directory")
parser.add_argument("-u", "--user", type=str, default='SYSDBA', help="Firebird DB username")
parser.add_argument("-p", "--password", type=str, default='masterkey', help="Firebird DB password")

parser.add_argument("-d", "--database", dest='path_to_db', type=str, required=True,
default="tests/employee.fdb",
help="Firebird server alias or database name (eg. employee) or full path if file, eg ./employee.fdb")

args = parser.parse_args()
if len(fbe_args) == 0: # Command line
args = parser.parse_args()
else: # From module
args = parser.parse_args( fbe_args[0])

return args
17 changes: 11 additions & 6 deletions src/fb_export.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/usr/bin/env python3
# Dump firebase data
# Export Firebird database contents to CSV

import fdb
from shutil import rmtree
import pandas as pd
import sys
import os
from pathlib import Path

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(SCRIPT_DIR))


from args import get_args
#import .args
from dftools import fix_fields
from utils import mkdirs

Expand All @@ -20,14 +26,13 @@
unzip_testdb()


def main():
args = get_args()

def main(*fbe_arg):
args = get_args(fbe_arg)
if os.getenv('GITHUB_ACTIONS'):
con = fdb.connect(args.path_to_db, user=args.user, password=args.password,
fb_library_name='/opt/firebird/lib/libfbembed.so')
else:
con = fdb.connect(args.path_to_db, user=args.user, password=args.password)
con = fdb.connect(args.path_to_db)

print("Connected to ", con.database_name, ' via ', con.firebird_version, file=sys.stderr)

Expand Down Expand Up @@ -122,7 +127,7 @@ def main():
except:
print("Error converting DF to json: " + table, file=sys.stderr)

if args.combine:
if args.join:
filename = dbf_name.rstrip('.fdb') + '.' + args.format
fmode = 'a'
else:
Expand Down

0 comments on commit 11cba5f

Please sign in to comment.