Skip to content

Commit

Permalink
Revoked new version 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ADv0rnik committed Sep 21, 2024
1 parent ee45a78 commit f879092
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 347 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tmp.py
*.Spe
*.spe
*.csv
*.log
122 changes: 86 additions & 36 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,104 @@
import argparse
import os
import queue
import sys
import threading
import pandas as pd

from pathlib import Path
from typing import List
from src.processor.data_parser import DataParser


BASE_DIR = Path(__file__).resolve().parent
SRC_DIR = BASE_DIR.joinpath("src")


def list_files() -> List[str]:
file_paths = [os.path.join(SRC_DIR, filename)
for filename in os.listdir(SRC_DIR)
if os.path.isfile(os.path.join(SRC_DIR, filename)) and filename.endswith(".Spe")]

return file_paths
from src.config import FFORMAT, VERSION, OUTPUT_FORMAT
from src.reader_logger import write_logs


def parse_arguments():
"""
The method determines parser for arguments derived from the CLI.
Defines all optional and positional arguments such as path, --version and --out
:returns: arguments passed on CLI script call
"""
parser = argparse.ArgumentParser(prog='spec-src',
description="List of commands for SpecReader",
epilog="Report about all bugs to [email protected]")
parser.add_argument(
"-v",
"--version",
help="Show this application's version and exit",
action="version",
version=f"SpecReader v.{VERSION}"
)
parser.add_argument(
"-o",
"--out",
help="Enable this option to define the output filename",
type=str,
default=f'output_data'
)
parser.add_argument(
"Path",
metavar='path',
type=str,
help='The path to spectrum file'
)
return parser.parse_args()


def list_files(source_path: str, file_format: str) -> List[str]:
file_list = []
for root, _, files in os.walk(source_path):
for file in files:
if file_format in file:
file_path = os.path.join(root, file)
file_list.append(file_path)
return file_list


def runner():
data_queue = queue.Queue()
data_parser = DataParser()
files = list_files()

threads = [threading.Thread(target=data_parser.read_from_file, args=(file, data_queue, )) for file in files]

for thread in threads:
thread.start()

for thread in threads:
thread.join()

results = {}
while not data_queue.empty():
results.update(data_queue.get())

return results

def save_to_dataframe(data):
try:
args = parse_arguments()
except argparse.ArgumentError as err:
print(f"[-] Wrong argument: {err}")
write_logs(f"{err}. End program", 'error')
sys.exit()
else:
data_queue = queue.Queue()
data_parser = DataParser()
src_path = args.Path

if args.out.endswith(OUTPUT_FORMAT):
output_dir = os.path.join(os.path.split(src_path)[0], args.out)
else:
output_dir = os.path.join(os.path.split(src_path)[0], args.out + OUTPUT_FORMAT)

files = list_files(src_path, FFORMAT)
threads = [threading.Thread(target=data_parser.read_from_file, args=(file, data_queue, )) for file in files]

for thread in threads:
thread.start()

for thread in threads:
thread.join()

results = {}
while not data_queue.empty():
results.update(data_queue.get())

dataframe = save_to_dataframe(output_dir, results)
return dataframe

def save_to_dataframe(output_dir, data):
df = pd.DataFrame.from_dict(data, orient='index')
df = df.transpose()
df = df.rename(index={df.index[-1]: "LT"})
df.to_csv(BASE_DIR.joinpath("data.csv"))
df.to_csv(output_dir)
print(f"[+] The data has been saved into: {output_dir}")
write_logs(f"[+] The data has been saved into: {output_dir}", 'info')
return df


if __name__ == "__main__":
print("[+] Run program")
res = runner()
# print(res)
save_to_dataframe(res)
write_logs("Start program")
runner()
Binary file modified requirements.txt
Binary file not shown.
14 changes: 7 additions & 7 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ platforms = unix, linux, win32
classifiers =
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12

[options]
zip_safe = False
Expand All @@ -22,11 +23,10 @@ packages = find:
include_package_data = True

install_requires =
art>=5.6
pandas>=1.4.3
matplotlib>=3.5.2
tqdm>=4.64.0
setuptools>=64.0.3
art
pandas
matplotlib
setuptools

[options.package_data]
reader =
Expand Down
14 changes: 14 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os
from pathlib import Path


VERSION = "0.2.1"
FFORMAT = ".Spe"
OUTPUT_FORMAT = ".csv"


BASE_DIR = Path(__file__).resolve().parent
DEFAULT_OUTPUT_DIR = BASE_DIR / "output"

if not os.path.isdir(DEFAULT_OUTPUT_DIR):
os.mkdir(DEFAULT_OUTPUT_DIR)
3 changes: 2 additions & 1 deletion src/processor/data_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __split_line(line: str) -> List[str]:
def read_from_file(self, file, queue_: Queue):
counts = []
head_tail = os.path.split(file)
dir_name = head_tail[0].split(os.sep)[-1]
with open(file, 'r') as f:
lines = [line.strip() for line in f]
for i, line in enumerate(lines):
Expand All @@ -26,4 +27,4 @@ def read_from_file(self, file, queue_: Queue):
else:
break
counts.append(live_time)
queue_.put({head_tail[1]: counts})
queue_.put({f"{dir_name}_{head_tail[1]}": counts})
154 changes: 0 additions & 154 deletions src/reader_core.py

This file was deleted.

6 changes: 2 additions & 4 deletions src/reader_logger.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import logging
import os

DEFAULT_DATA_DIR = os.path.dirname(__file__) + "/data"
from src.config import DEFAULT_OUTPUT_DIR


def write_logs(text: str, level_type="info"):
logger = logging
logger_format = "%(levelname)s %(asctime)s: %(message)s"
logger.basicConfig(filename=DEFAULT_DATA_DIR + "/reader_logs.log",
logger.basicConfig(filename=DEFAULT_OUTPUT_DIR.joinpath("reader_logs.log"),
filemode="w",
level=logging.INFO,
format=logger_format,
Expand Down
Loading

0 comments on commit f879092

Please sign in to comment.