Skip to content

Commit

Permalink
fix(pain001): codacy fixes mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Sep 15, 2023
1 parent 3332b2a commit e102b05
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
1 change: 0 additions & 1 deletion pain001/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
# limitations under the License.

"""The Python pain001 module."""
__all__ = ["pain001"]
__version__ = "0.0.23"
6 changes: 1 addition & 5 deletions pain001/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ def check_variable(variable, name):
):
print(click.get_current_context().get_help())
sys.exit(1)
"""
Entrypoint for pain001 when invoked as a module with
python3 -m pain001 <xml_message_type> <xml_template_file_path>
<xsd_schema_file_path> <data_file_path>.
"""

logger = Context.get_instance().get_logger()

logger.info("Parsing command line arguments.")
Expand Down
30 changes: 30 additions & 0 deletions pain001/db/load_db_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@
import sqlite3
import os

def sanitize_table_name(table_name):
"""
Sanitize a table name by replacing all characters that are not alphanumeric or underscores with underscores.
Args:
table_name (str): The table name to sanitize.
Returns:
str: The sanitized table name.
"""
sanitized_name = ""
for char in table_name:
if char.isalnum() or char == '_':
sanitized_name += char
else:
sanitized_name += '_'

# Ensure the resulting name starts with an alphabetic character
if not sanitized_name[0].isalpha():
sanitized_name = 'table_' + sanitized_name

return sanitized_name

def load_db_data(data_file_path, table_name):
"""
Expand All @@ -41,14 +63,20 @@ def load_db_data(data_file_path, table_name):
Example:
data = load_db_data("my_database.db", "my_table")
"""

# Check if the SQLite file exists
if not os.path.exists(data_file_path):
raise FileNotFoundError(
f"SQLite file '{data_file_path}' does not exist."
)

# Connect to the SQLite database
conn = sqlite3.connect(data_file_path)
cursor = conn.cursor()

# Sanitize the table_name before using it in the query
table_name = sanitize_table_name(table_name)

# Fetch column names from the table
cursor.execute("PRAGMA table_info({})".format(table_name))
columns = [column[1] for column in cursor.fetchall()]
Expand All @@ -66,6 +94,8 @@ def load_db_data(data_file_path, table_name):
row_dict[columns[i]] = value
data.append(row_dict)

# Close the connection to the SQLite database
conn.close()

return data

15 changes: 15 additions & 0 deletions pain001/db/validate_db_data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Copyright (C) 2023 Sebastien Rousseau.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def validate_db_data(data):
"""Validate the SQLite data before processing it.
Expand Down
4 changes: 2 additions & 2 deletions templates/pain.001.001.03/pain.001.001.03.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<InitgPty>
<Nm>John Doe</Nm>
<PstlAdr>
<StrtNm>John's Street</StrtNm>
<StrtNm>John&#39;s Street</StrtNm>
<BldgNb>1</BldgNb>
<PstCd>12345</PstCd>
<TwnNm>John's Town</TwnNm>
<TwnNm>John&#39;s Town</TwnNm>
<Ctry>DE</Ctry>
</PstlAdr>
</InitgPty>
Expand Down

0 comments on commit e102b05

Please sign in to comment.