Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cogs/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from cogs.exceptions import AddError


def maybe_update_fields(headers):
def maybe_update_fields(cogs_dir, headers):
"""Check fieldnames in headers and add to field.tsv if they do not exist."""
fields = get_fields()
fields = get_fields(cogs_dir)
update_fields = False
for h in headers:
field = re.sub(r"[^A-Za-z0-9]+", "_", h.lower()).strip("_")
Expand All @@ -19,7 +19,7 @@ def maybe_update_fields(headers):

# Update field.tsv if we need to
if update_fields:
with open(".cogs/field.tsv", "w") as f:
with open(f"{cogs_dir}/field.tsv", "w") as f:
writer = csv.DictWriter(
f,
delimiter="\t",
Expand All @@ -35,7 +35,7 @@ def maybe_update_fields(headers):
def add(path, title=None, description=None, freeze_row=0, freeze_column=0, verbose=False):
"""Add a table (TSV or CSV) to the COGS project. This updates sheet.tsv and field.tsv."""
set_logging(verbose)
validate_cogs_project()
cogs_dir = validate_cogs_project()

# Open the provided file and make sure we can parse it as TSV or CSV
if path.endswith(".csv"):
Expand All @@ -56,7 +56,7 @@ def add(path, title=None, description=None, freeze_row=0, freeze_column=0, verbo
title = ntpath.basename(path).split(".")[0]

# Make sure we aren't duplicating a table
local_sheets = get_tracked_sheets()
local_sheets = get_tracked_sheets(cogs_dir)
if title in local_sheets:
raise AddError(f"'{title}' sheet already exists in this project")

Expand All @@ -71,10 +71,10 @@ def add(path, title=None, description=None, freeze_row=0, freeze_column=0, verbo
description = ""

if headers:
maybe_update_fields(headers)
maybe_update_fields(cogs_dir, headers)

# Finally, add this TSV to sheet.tsv
with open(".cogs/sheet.tsv", "a") as f:
with open(f"{cogs_dir}/sheet.tsv", "a") as f:
writer = csv.DictWriter(
f,
delimiter="\t",
Expand Down
24 changes: 12 additions & 12 deletions cogs/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
message_headers = ["table", "cell", "level", "rule id", "rule", "message", "suggestion"]


def apply_data_validation(data_valiation_tables):
def apply_data_validation(cogs_dir, data_valiation_tables):
"""Apply one or more data validation rules to the sheets."""
tracked_sheets = get_tracked_sheets()
tracked_sheets = get_tracked_sheets(cogs_dir)
add_dv_rules = {}
for data_validation_table in data_valiation_tables:
add_rows = []
Expand All @@ -77,14 +77,14 @@ def apply_data_validation(data_valiation_tables):
dv_rules.append(row)
add_dv_rules[sheet_title] = dv_rules

update_data_validation(add_dv_rules, [])
update_data_validation(cogs_dir, add_dv_rules, [])


def apply_messages(message_tables):
def apply_messages(cogs_dir, message_tables):
"""Apply one or more message tables (from dict reader) to the sheets as formats and notes."""
tracked_sheets = get_tracked_sheets()
tracked_sheets = get_tracked_sheets(cogs_dir)
# Get existing formats
sheet_to_formats = get_sheet_formats()
sheet_to_formats = get_sheet_formats(cogs_dir)

# Remove any formats that are "applied" (format ID 0, 1, or 2)
sheet_to_manual_formats = {}
Expand All @@ -97,7 +97,7 @@ def apply_messages(message_tables):
sheet_to_formats = sheet_to_manual_formats

# Remove any notes that are "applied" (starts with ERROR, WARN, or INFO)
sheet_to_notes = get_sheet_notes()
sheet_to_notes = get_sheet_notes(cogs_dir)
sheet_to_manual_notes = {}
for sheet_title, cell_to_notes in sheet_to_notes.items():
manual_notes = {}
Expand Down Expand Up @@ -209,8 +209,8 @@ def apply_messages(message_tables):
sheet_to_notes[table] = cell_to_notes

# Update formats & notes TSVs
update_note(sheet_to_notes, [])
update_format(sheet_to_formats, [])
update_note(cogs_dir, sheet_to_notes, [])
update_format(cogs_dir, sheet_to_formats, [])


def clean_rule(sheet_title, loc, condition, value):
Expand Down Expand Up @@ -249,7 +249,7 @@ def clean_rule(sheet_title, loc, condition, value):
def apply(paths, verbose=False):
"""Apply a table to the spreadsheet. The type of table to 'apply' is based on the headers:
standardized messages or data validation."""
validate_cogs_project()
cogs_dir = validate_cogs_project()
set_logging(verbose)

message_tables = []
Expand Down Expand Up @@ -279,7 +279,7 @@ def apply(paths, verbose=False):
raise ApplyError(f"The headers in table {p} are not valid for apply")

if message_tables:
apply_messages(message_tables)
apply_messages(cogs_dir, message_tables)

if data_validation_tables:
apply_data_validation(data_validation_tables)
apply_data_validation(cogs_dir, data_validation_tables)
34 changes: 17 additions & 17 deletions cogs/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
)


def clear_data_validation(sheet_title):
def clear_data_validation(cogs_dir, sheet_title):
"""Remove all data validation rules from a sheet."""
sheet_dv_rules = get_data_validation()
sheet_dv_rules = get_data_validation(cogs_dir)
logging.info(f"removing all data validation rules from '{sheet_title}'")
if sheet_title in sheet_dv_rules:
del sheet_dv_rules[sheet_title]
Expand All @@ -24,7 +24,7 @@ def clear_data_validation(sheet_title):
for row in dv_rules:
row["Sheet Title"] = sheet_title
dv_rows.append(row)
with open(".cogs/validation.tsv", "w") as f:
with open(f"{cogs_dir}/validation.tsv", "w") as f:
writer = csv.DictWriter(
f,
delimiter="\t",
Expand All @@ -35,9 +35,9 @@ def clear_data_validation(sheet_title):
writer.writerows(dv_rows)


def clear_formats(sheet_title):
def clear_formats(cogs_dir, sheet_title):
"""Remove all formats from a sheet."""
sheet_to_formats = get_sheet_formats()
sheet_to_formats = get_sheet_formats(cogs_dir)
logging.info(f"removing all formats from '{sheet_title}'")
if sheet_title in sheet_to_formats:
del sheet_to_formats[sheet_title]
Expand All @@ -46,17 +46,17 @@ def clear_formats(sheet_title):
for sheet_title, formats in sheet_to_formats.items():
for cell, fmt in formats.items():
fmt_rows.append({"Sheet Title": sheet_title, "Cell": cell, "Format ID": fmt})
with open(".cogs/format.tsv", "w") as f:
with open(f"{cogs_dir}/format.tsv", "w") as f:
writer = csv.DictWriter(
f, delimiter="\t", lineterminator="\n", fieldnames=["Sheet Title", "Cell", "Format ID"],
)
writer.writeheader()
writer.writerows(fmt_rows)


def clear_notes(sheet_title):
def clear_notes(cogs_dir, sheet_title):
"""Remove all notes from a sheet."""
sheet_to_notes = get_sheet_notes()
sheet_to_notes = get_sheet_notes(cogs_dir)
logging.info(f"removing all notes from '{sheet_title}'")
if sheet_title in sheet_to_notes:
del sheet_to_notes[sheet_title]
Expand All @@ -65,7 +65,7 @@ def clear_notes(sheet_title):
for sheet_title, notes in sheet_to_notes.items():
for cell, note in notes.items():
note_rows.append({"Sheet Title": sheet_title, "Cell": cell, "Note": note})
with open(".cogs/note.tsv", "w") as f:
with open(f"{cogs_dir}/note.tsv", "w") as f:
writer = csv.DictWriter(
f, delimiter="\t", lineterminator="\n", fieldnames=["Sheet Title", "Cell", "Note"],
)
Expand All @@ -75,11 +75,11 @@ def clear_notes(sheet_title):

def clear(keyword, on_sheets=None, verbose=False):
"""Remove formats, notes, and/or data validation rules from one or more sheets."""
validate_cogs_project()
cogs_dir = validate_cogs_project()
set_logging(verbose)

# Validate sheets
tracked_sheets = get_tracked_sheets()
tracked_sheets = get_tracked_sheets(cogs_dir)

if not on_sheets:
# If no sheet was supplied, clear from all
Expand All @@ -96,17 +96,17 @@ def clear(keyword, on_sheets=None, verbose=False):

if keyword == "formats":
for st in on_sheets:
clear_formats(st)
clear_formats(cogs_dir, st)
elif keyword == "notes":
for st in on_sheets:
clear_notes(st)
clear_notes(cogs_dir, st)
elif keyword == "validation":
for st in on_sheets:
clear_data_validation(st)
clear_data_validation(cogs_dir, st)
elif keyword == "all":
for st in on_sheets:
clear_formats(st)
clear_notes(st)
clear_data_validation(st)
clear_formats(cogs_dir, st)
clear_notes(cogs_dir, st)
clear_data_validation(cogs_dir, st)
else:
raise ClearError("Unknown keyword: " + keyword)
8 changes: 4 additions & 4 deletions cogs/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def delete(verbose=False):
"""Read COGS configuration and delete the spreadsheet corresponding to the spreadsheet ID.
Remove .cogs directory."""
set_logging(verbose)
validate_cogs_project()
config = get_config()
cogs_dir = validate_cogs_project()
config = get_config(cogs_dir)

# Get a client to perform Sheet actions
gc = get_client_from_config(config)
Expand All @@ -29,5 +29,5 @@ def delete(verbose=False):
logging.info(f"successfully deleted Google Sheet '{title}' ({ssid})")

# Remove the COGS data
if os.path.exists(".cogs"):
shutil.rmtree(".cogs")
if os.path.exists(cogs_dir):
shutil.rmtree(cogs_dir)
16 changes: 8 additions & 8 deletions cogs/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def close_screen(stdscr):
curses.endwin()


def get_diff_lines(diffs, sheet_details):
def get_diff_lines(cogs_dir, diffs, sheet_details):
"""Return the lines for a diff as an array of pairs (text, formatting)."""
lines = []
for sheet_title, sheet_diff in diffs.items():
details = sheet_details[sheet_title]
path_name = re.sub(r"[^A-Za-z0-9]+", "_", sheet_title.lower())
remote = f".cogs/tracked/{path_name}.tsv"
remote = f"{cogs_dir}/tracked/{path_name}.tsv"
local = details["Path"]
if len(sheet_diff) > 1:
lines.append(("", None))
Expand Down Expand Up @@ -60,7 +60,7 @@ def get_diff_lines(diffs, sheet_details):
return lines


def display_diff(diffs, sheets):
def display_diff(cogs_dir, diffs, sheets):
"""Display an interactive curses screen with the formatted daff diff lines."""
# Init the curses screen
stdscr = curses.initscr()
Expand All @@ -80,7 +80,7 @@ def display_diff(diffs, sheets):
curses.init_pair(3, curses.COLOR_CYAN, -1)

# Get the lines to display
lines = get_diff_lines(diffs, sheets)
lines = get_diff_lines(cogs_dir, diffs, sheets)

if not lines:
# Nothing to display
Expand Down Expand Up @@ -199,9 +199,9 @@ def diff(paths=None, use_screen=True, verbose=False):
"""Return a dict of sheet title to daff diff lines. If no paths are provided, diff over all
sheets in the project. If use_screen, display an interactive curses screen with the diffs."""
set_logging(verbose)
validate_cogs_project()
cogs_dir = validate_cogs_project()

sheets = get_tracked_sheets()
sheets = get_tracked_sheets(cogs_dir)
tracked_paths = [details["Path"] for details in sheets.values()]
if paths:
# Update sheets to diff
Expand All @@ -217,7 +217,7 @@ def diff(paths=None, use_screen=True, verbose=False):
diffs = {}
for sheet_title, details in sheets.items():
path_name = re.sub(r"[^A-Za-z0-9]+", "_", sheet_title.lower())
remote = f".cogs/tracked/{path_name}.tsv"
remote = f"{cogs_dir}/tracked/{path_name}.tsv"
local = details["Path"]
if os.path.exists(local) and os.path.exists(remote):
sheet_diff = get_diff(local, remote)
Expand All @@ -227,6 +227,6 @@ def diff(paths=None, use_screen=True, verbose=False):
return None

if use_screen:
return display_diff(diffs, sheets)
return display_diff(cogs_dir, diffs, sheets)

return diffs
Loading