first commit
This commit is contained in:
commit
2e8b8a5e75
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
sql_config.json
|
||||||
|
bfg.jar
|
||||||
|
/build
|
||||||
|
/dist
|
||||||
|
*.spec
|
||||||
|
__pycache__/
|
||||||
|
/output
|
17
LICENSE
Normal file
17
LICENSE
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
|
||||||
|
Copyright 2023 OceanSlim
|
||||||
|
[pySource]
|
||||||
|
|
||||||
|
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.
|
32
README.md
Normal file
32
README.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Alpine ITWs iSource Inventory Management CLI
|
||||||
|
This command-line interface (CLI) provides functionalities to analyze and manage the Alpine ITWs iSource inventory management system.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
Python 3.x: Ensure Python 3.x is installed on your system.
|
||||||
|
SQL Server Credentials: Have SQL Server credentials available to configure the CLI.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
Clone the Repository:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
Copy code
|
||||||
|
git clone https://github.com/0ceanslim/pysource.git
|
||||||
|
cd pysource
|
||||||
|
```
|
||||||
|
|
||||||
|
Run the CLI
|
||||||
|
`python cli.py`
|
||||||
|
|
||||||
|
- Follow the prompts to enter your SQL Server credentials when prompted.
|
||||||
|
- Once completed, you're ready to proceed.
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
If you'd like to contribute to this project:
|
||||||
|
|
||||||
|
1. Fork the repository and create a new branch.
|
||||||
|
2. Make your changes and commit them.
|
||||||
|
3. Push to the branch and submit a pull request detailing your changes.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0) - see the [LICENSE](LICENSE) file for details.
|
115
cli.py
Normal file
115
cli.py
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
import os
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from src.help import help
|
||||||
|
|
||||||
|
|
||||||
|
def clear_console():
|
||||||
|
# Clear console based on the operating system
|
||||||
|
if os.name == "nt": # For Windows
|
||||||
|
os.system("cls")
|
||||||
|
else: # For Unix/Linux/Mac
|
||||||
|
os.system("clear")
|
||||||
|
|
||||||
|
|
||||||
|
def check_python_installation():
|
||||||
|
try:
|
||||||
|
python_version = subprocess.check_output(
|
||||||
|
["python", "--version"], stderr=subprocess.STDOUT, universal_newlines=True
|
||||||
|
)
|
||||||
|
print(f"Python is already installed ({python_version})")
|
||||||
|
return True
|
||||||
|
except FileNotFoundError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def install_python():
|
||||||
|
print("Python is not installed. Please download and install it from:")
|
||||||
|
print("https://www.python.org/downloads/")
|
||||||
|
input("Press Enter to continue after installing Python...")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def check_sql_config():
|
||||||
|
if os.path.exists("sql_config.json"):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_sql_config():
|
||||||
|
# Define the connection string
|
||||||
|
sql_server = input("Enter the sql server (network\location): ")
|
||||||
|
sql_database = input("Enter the sql database: ")
|
||||||
|
sql_user = input("Enter the sql user: ")
|
||||||
|
sql_password = input("Enter the sql password: ")
|
||||||
|
|
||||||
|
sql_config = {
|
||||||
|
"sql_server": sql_server,
|
||||||
|
"sql_database": sql_database,
|
||||||
|
"sql_user": sql_user,
|
||||||
|
"sql_password": sql_password,
|
||||||
|
}
|
||||||
|
|
||||||
|
with open("sql_config.json", "w") as config_file:
|
||||||
|
json.dump(sql_config, config_file)
|
||||||
|
|
||||||
|
|
||||||
|
def run_script(script_name):
|
||||||
|
script_path = os.path.join("src", f"{script_name}.py")
|
||||||
|
|
||||||
|
if os.path.exists(script_path):
|
||||||
|
# print(f"Running {script_name}...")
|
||||||
|
os.system(f"python {script_path}")
|
||||||
|
else:
|
||||||
|
print(f"Error: {script_name} not found!")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
setup_completed = False
|
||||||
|
|
||||||
|
while not check_python_installation():
|
||||||
|
install_python()
|
||||||
|
|
||||||
|
subprocess.run(["pip", "install", "-r", "requirements.txt"])
|
||||||
|
|
||||||
|
while not check_sql_config():
|
||||||
|
clear_console()
|
||||||
|
print("sql config file not found.")
|
||||||
|
user_input = input(
|
||||||
|
"Do you have credentials for your SQL Server (yes/no)? "
|
||||||
|
).lower()
|
||||||
|
|
||||||
|
if user_input in ["no", "n"]:
|
||||||
|
print(
|
||||||
|
"Please retrieve your server location and credentials as well as the name of the database you'd like to work with"
|
||||||
|
)
|
||||||
|
input("Press Enter to continue after you have this information...")
|
||||||
|
elif user_input in ["yes", "y"]:
|
||||||
|
get_sql_config()
|
||||||
|
else:
|
||||||
|
print("Invalid input. Please enter 'y' or 'n'.")
|
||||||
|
|
||||||
|
setup_completed = True
|
||||||
|
|
||||||
|
print("You are ready to proceed.")
|
||||||
|
clear_console()
|
||||||
|
|
||||||
|
print("\nPySource Command Line Interface")
|
||||||
|
help()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
user_input = input("Enter a command: ")
|
||||||
|
|
||||||
|
if user_input.lower() == "exit":
|
||||||
|
break
|
||||||
|
if user_input.lower() == "help":
|
||||||
|
help()
|
||||||
|
if setup_completed:
|
||||||
|
run_script(user_input)
|
||||||
|
else:
|
||||||
|
print("Error: Setup not completed. Use 'help' to see available commands.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
pyodbc
|
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
8
src/help.py
Normal file
8
src/help.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
def help():
|
||||||
|
print("\nAvailable Commands:\n")
|
||||||
|
print("listDatabases - Lists all Databases in the SQL Server as defined in your sql_config.json")
|
||||||
|
print("listTables - Lists all tables in the Database that's configured in your sql_config.json")
|
||||||
|
print("productData - Saves a CSV with data for all products to the Output folder with the following information: \n tProduct_ID,Sku,Product_Type,Product_Code,ShortDescription,IsActive")
|
||||||
|
print("skuCost - Saves a CSV file for each Product Category to the Output folder that contains the items sku and associated cost")
|
||||||
|
print("help - Display available commands")
|
||||||
|
print("exit - Quit the program\n")
|
30
src/listDatabases.py
Normal file
30
src/listDatabases.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import pyodbc
|
||||||
|
from util.sqlHandler import read_sql_config
|
||||||
|
|
||||||
|
# Read SQL credentials from the JSON file
|
||||||
|
sql_server, sql_database, sql_user, sql_password, connection_string = read_sql_config()
|
||||||
|
|
||||||
|
# Connect to the database
|
||||||
|
try:
|
||||||
|
connection = pyodbc.connect(connection_string)
|
||||||
|
except pyodbc.Error as e:
|
||||||
|
print(f"Error connecting to the database: {e}")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# Define the SQL query
|
||||||
|
sql_query = "SELECT name FROM sys.databases;"
|
||||||
|
|
||||||
|
# Execute the query and fetch the results
|
||||||
|
try:
|
||||||
|
cursor = connection.cursor()
|
||||||
|
cursor.execute(sql_query)
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
|
||||||
|
for row in rows:
|
||||||
|
print(row.name)
|
||||||
|
except pyodbc.Error as e:
|
||||||
|
print(f"Error executing the query: {e}")
|
||||||
|
finally:
|
||||||
|
# Close the cursor and connection
|
||||||
|
cursor.close()
|
||||||
|
connection.close()
|
31
src/listTables.py
Normal file
31
src/listTables.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import pyodbc
|
||||||
|
from util.sqlHandler import read_sql_config
|
||||||
|
|
||||||
|
# Read SQL credentials from the JSON file
|
||||||
|
sql_server, sql_database, sql_user, sql_password, connection_string = read_sql_config()
|
||||||
|
|
||||||
|
# Connect to the database
|
||||||
|
try:
|
||||||
|
connection = pyodbc.connect(connection_string)
|
||||||
|
except pyodbc.Error as e:
|
||||||
|
print(f"Error connecting to the database: {e}")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# Get a list of all tables in the database
|
||||||
|
tables = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
cursor = connection.cursor()
|
||||||
|
cursor.execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';")
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
tables = [row.TABLE_NAME for row in rows]
|
||||||
|
except pyodbc.Error as e:
|
||||||
|
print(f"Error fetching table names: {e}")
|
||||||
|
|
||||||
|
# Print the names of all tables
|
||||||
|
for table in tables:
|
||||||
|
print(table)
|
||||||
|
|
||||||
|
# Close the cursor and connection
|
||||||
|
cursor.close()
|
||||||
|
connection.close()
|
51
src/productData.py
Normal file
51
src/productData.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import pyodbc
|
||||||
|
import csv
|
||||||
|
from util.sqlHandler import read_sql_config
|
||||||
|
|
||||||
|
# Read SQL credentials from the JSON file
|
||||||
|
sql_server, sql_database, sql_user, sql_password, connection_string = read_sql_config()
|
||||||
|
|
||||||
|
# Connect to the database
|
||||||
|
try:
|
||||||
|
connection = pyodbc.connect(connection_string)
|
||||||
|
except pyodbc.Error as e:
|
||||||
|
print(f"Error connecting to the database: {e}")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# Define the table name and columns of interest
|
||||||
|
table_name = 'tProduct'
|
||||||
|
columns_of_interest = ['tProduct_ID', 'Sku', 'Product_Type', 'Product_Code', 'ShortDescription', 'IsActive']
|
||||||
|
|
||||||
|
# Retrieve data from the specified columns where IsActive=1
|
||||||
|
data = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
cursor = connection.cursor()
|
||||||
|
column_names = ', '.join(columns_of_interest)
|
||||||
|
query = f"SELECT {column_names} FROM {table_name} WHERE IsActive = 1;"
|
||||||
|
cursor.execute(query)
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
data = [list(row) for row in rows]
|
||||||
|
except pyodbc.Error as e:
|
||||||
|
print(f"Error fetching data: {e}")
|
||||||
|
|
||||||
|
# Define the CSV file path
|
||||||
|
csv_file_path = 'output/tProduct_data.csv'
|
||||||
|
|
||||||
|
# Save the retrieved data to a CSV file
|
||||||
|
try:
|
||||||
|
with open(csv_file_path, 'w', newline='') as csv_file:
|
||||||
|
csv_writer = csv.writer(csv_file)
|
||||||
|
|
||||||
|
# Write the header row
|
||||||
|
csv_writer.writerow(columns_of_interest)
|
||||||
|
|
||||||
|
# Write the data rows
|
||||||
|
csv_writer.writerows(data)
|
||||||
|
print(f"Data saved to {csv_file_path}")
|
||||||
|
except IOError as e:
|
||||||
|
print(f"Error saving data to CSV: {e}")
|
||||||
|
|
||||||
|
# Close the cursor and connection
|
||||||
|
cursor.close()
|
||||||
|
connection.close()
|
58
src/skuCost.py
Normal file
58
src/skuCost.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import pyodbc
|
||||||
|
import csv
|
||||||
|
import os
|
||||||
|
from util.sqlHandler import read_sql_config
|
||||||
|
|
||||||
|
# Read SQL credentials from the JSON file
|
||||||
|
sql_server, sql_database, sql_user, sql_password, connection_string = read_sql_config()
|
||||||
|
|
||||||
|
# Connect to the database
|
||||||
|
try:
|
||||||
|
connection = pyodbc.connect(connection_string)
|
||||||
|
except pyodbc.Error as e:
|
||||||
|
print(f"Error connecting to the database: {e}")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# Define the directory to save CSV files
|
||||||
|
output_directory = 'output/product_type_csv'
|
||||||
|
os.makedirs(output_directory, exist_ok=True)
|
||||||
|
|
||||||
|
# Define the SQL query to retrieve data
|
||||||
|
query = """
|
||||||
|
SELECT tProduct.Product_Type, tProduct.Sku, tCost.Cost
|
||||||
|
FROM tProduct
|
||||||
|
INNER JOIN tProductgroup_Product
|
||||||
|
ON tProduct.tProduct_ID = tProductgroup_Product.tProduct_ID
|
||||||
|
INNER JOIN tCost
|
||||||
|
ON tProductgroup_Product.tProductgroup_Product_ID = tCost.tProductgroup_Product_ID
|
||||||
|
WHERE tProduct.IsActive = 1
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Execute the SQL query
|
||||||
|
try:
|
||||||
|
cursor = connection.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
|
||||||
|
# Fetch the data and group by Product_Type
|
||||||
|
data = {}
|
||||||
|
for row in cursor.fetchall():
|
||||||
|
product_type, sku, cost = row
|
||||||
|
if product_type not in data:
|
||||||
|
data[product_type] = []
|
||||||
|
data[product_type].append((sku, cost))
|
||||||
|
except pyodbc.Error as e:
|
||||||
|
print(f"Error executing SQL query: {e}")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# Close the cursor and connection
|
||||||
|
cursor.close()
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
# Save data to separate CSV files for each Product_Type
|
||||||
|
for product_type, rows in data.items():
|
||||||
|
csv_file_path = os.path.join(output_directory, f"{product_type}_data.csv")
|
||||||
|
with open(csv_file_path, 'w', newline='') as csv_file:
|
||||||
|
csv_writer = csv.writer(csv_file)
|
||||||
|
csv_writer.writerow(['Sku', 'Cost'])
|
||||||
|
csv_writer.writerows(rows)
|
||||||
|
print(f"Data for {product_type} saved to {csv_file_path}")
|
0
src/util/__init__.py
Normal file
0
src/util/__init__.py
Normal file
13
src/util/sqlHandler.py
Normal file
13
src/util/sqlHandler.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
def read_sql_config(filename="sql_config.json"):
|
||||||
|
with open(filename, "r") as config_file:
|
||||||
|
config = json.load(config_file)
|
||||||
|
|
||||||
|
sql_server = config["sql_server"]
|
||||||
|
sql_database = config["sql_database"]
|
||||||
|
sql_user = config["sql_user"]
|
||||||
|
sql_password = config["sql_password"]
|
||||||
|
connection_string = f'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={sql_server};DATABASE={sql_database};UID={sql_user};PWD={sql_password}'
|
||||||
|
|
||||||
|
return sql_server, sql_database, sql_user, sql_password, connection_string
|
Loading…
Reference in New Issue
Block a user