Skip to content

Commit c30e7b6

Browse files
author
Jesse
committed
bug fixes and functionality from issue #2
1 parent a4ac735 commit c30e7b6

File tree

8 files changed

+78
-51
lines changed

8 files changed

+78
-51
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.csv
44
*.xlsx
55
temp/
6+
test.py
67

78
# Byte-compiled / optimized / DLL files
89
__pycache__/

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog - xer-reader
22

3+
## 0.4.0 - 2024-11-21
4+
5+
* Reference [Issue #2](https://github.com/jjCode01/xer-reader/issues/2)
6+
* The `to_dict` method no longer throws an error if an unreckognized table is found; it now just ignores the table.
7+
* Added `FINTMPL` to table_data.
8+
* Updated `to_csv` method:
9+
* Can now pass a list of table names to export to CSV. For example pass in agrument `table_names=["TASK", "PROJWBS"]` to only export the task and wbs tables to CSV.
10+
* Can now change the delimeter. For example, pass in argument `delimeter=","` to use a coma rather than the default tab.
11+
12+
---
13+
314
## 0.3.2 - 2024-10-05
415

516
Clean up code.

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ Return True if table (`table_name`) if found in the XER file.
7474
**`parse_tables()`** -> *dict[str, Table]*
7575
Returns a dictionary with the table name as the key and a `Table` object as the value.
7676

77-
**`to_csv(file_directory: str | Path)`** -> *None*
78-
Generate a CSV file for each table in the XER file using 'tab' as the delimiter. CSV files will be created in the current working directory.
79-
Optional: Pass a string or Path object (`file_directory`) to speficy a folder to store the CSV files in.
77+
**`to_csv(file_directory: str | Path, table_names: list[str], delimeter: str)`** -> *None*
78+
Generate a CSV file for each table in the XER file. CSV files will be created in the current working directory.
79+
Optional `file_directory`: Pass a string or Path object to speficy a folder to store the CSV files in.
80+
Optional `table_names`: List of tables names to save to CSV files.
81+
Optional `delimeter`: Change the default delimeter from a `tab` to another string (e.g. a coma ",").
82+
83+
```python
84+
reader.to_csv(table_names=["TASK", "PROJWBS"], delimeter=",")
85+
```
8086

8187
**`to_excel()`** -> *None*
8288
Generate an Excel (.xlsx) file with each table in the XER file on its own spreadsheet. The Excel file will be create in the

poetry.lock

+13-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "xer-reader"
3-
version = "0.3.2"
3+
version = "0.4.0"
44
description = "Read and parse a Primavera P6 xer file."
55
authors = ["Jesse Jones <code@seqmanagement.com>"]
66
license = "GPL-3.0-only"

tests/test_reader.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,25 @@ def test_get_table_str(self):
7575
self.assertEqual(reader.get_table_str("PROJECT")[:7], "proj_id")
7676
self.assertEqual(reader.get_table_str("PROJWBS")[:6], "wbs_id")
7777

78-
# def test_to_csv(self):
79-
# print(f"Running to_csv tests on {len(self.files)} .xer files.")
80-
# for file in tqdm(self.files):
81-
# reader = XerReader(file)
82-
# tables = reader.to_dict()
83-
# temp_folder = Path.cwd().joinpath("temp")
84-
# if not temp_folder.is_dir():
85-
# Path.mkdir(temp_folder)
86-
87-
# reader.to_csv(temp_folder)
88-
89-
# for table_name in tables.keys():
90-
# csv_file = temp_folder.joinpath(f"{reader.file_name}_{table_name}.csv")
91-
# self.assertTrue(
92-
# csv_file.is_file(),
93-
# f"{table_name}.csv",
94-
# )
95-
# if csv_file.is_file():
96-
# Path.unlink(csv_file)
78+
def test_to_csv(self):
79+
print(f"Running to_csv tests on {len(self.files)} .xer files.")
80+
for file in tqdm(self.files):
81+
reader = XerReader(file)
82+
tables = reader.to_dict()
83+
temp_folder = Path.cwd().joinpath("temp")
84+
if not temp_folder.is_dir():
85+
Path.mkdir(temp_folder)
86+
87+
reader.to_csv(temp_folder)
88+
89+
for table_name in tables.keys():
90+
csv_file = temp_folder.joinpath(f"{reader.file_name}_{table_name}.csv")
91+
self.assertTrue(
92+
csv_file.is_file(),
93+
f"{table_name}.csv",
94+
)
95+
if csv_file.is_file():
96+
Path.unlink(csv_file)
9797

9898
# def test_to_excel(self):
9999
# print(f"Running to_excel tests on {len(self.files)} .xer files.")

xer_reader/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.3.2"
1+
__version__ = "0.4.0"
22

33
from xer_reader.src.reader import XerReader # noqa: F401
44
from xer_reader.src.table import XerTable # noqa: F401

xer_reader/src/reader.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from openpyxl import Workbook
1515
from openpyxl.worksheet.table import Table
1616

17-
from xer_reader.src.table import XerTable, UnrecognizedTable
17+
from xer_reader.src.table import XerTable, UnrecognizedTable
1818
from xer_reader.src.table_data import table_data
1919

2020
DATE_FORMAT = "%Y-%m-%d"
@@ -157,22 +157,32 @@ def to_dict(self) -> dict[str, XerTable]:
157157
table = XerTable(table_str)
158158
tables[table.name] = table
159159
except UnrecognizedTable:
160-
continue
160+
continue
161161
return tables
162162

163-
def to_csv(self, file_directory: str | Path = Path.cwd(), tables: list[str] = []) -> None:
163+
def to_csv(
164+
self,
165+
file_directory: str | Path = Path.cwd(),
166+
table_names: list[str] = [],
167+
delimeter: str = "\t",
168+
) -> None:
164169
"""
165170
Generate a CSV file for each table in the XER file.
166-
Uses `tab` as the delimiter.
171+
Uses `tab` as the delimiter by default.
167172
168173
Args:
169-
file_directory (str | Path, optional): Directory to save CSV files.
170-
Defaults to current working directory.
174+
file_directory (str | Path, optional): Directory to save CSV files. [Defaults to current working directory]
175+
table_names (list, optional): List of table names to save to CSV files. If empty, all tables will be saved.
176+
delimeter (str, optional): CSV delimeter. [Default is a `tab`]
171177
"""
172-
for name, table in self.to_dict().entries():
173-
if not tables or name in tables:
178+
names = [name.upper() for name in table_names]
179+
for table in self.to_dict().values():
180+
if not table_names or table.name in names:
174181
_write_table_to_csv(
175-
f"{self.file_name}_{table.name}", table, Path(file_directory)
182+
f"{self.file_name}_{table.name}",
183+
table,
184+
Path(file_directory),
185+
delimeter,
176186
)
177187

178188
def to_excel(self, file_directory: str | Path = Path.cwd()) -> None:
@@ -259,9 +269,11 @@ def _read_file(file: str | Path | BinaryIO) -> tuple[str, str]:
259269
return file_name, file_contents
260270

261271

262-
def _write_table_to_csv(name: str, table: XerTable, file_directory: Path) -> None:
272+
def _write_table_to_csv(
273+
name: str, table: XerTable, file_directory: Path, delimeter
274+
) -> None:
263275
with file_directory.joinpath(f"{name}.csv").open("w") as f:
264-
writer = csv.writer(f, delimiter="\t")
276+
writer = csv.writer(f, delimiter=delimeter)
265277
writer.writerow(table.labels)
266278
for row in table.rows:
267279
writer.writerow(row)

0 commit comments

Comments
 (0)