Skip to content

Flexible export #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 24 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import traceback
import sys

from src import commits_parser
from src import contributors_parser
Expand All @@ -10,7 +11,7 @@
from src import pull_requests_parser
from src import wikipars
from src import workflow_runs_parser
from src.utils import parse_time
from src.utils import parse_time, validate_and_normalize_cell


def parse_args():
Expand All @@ -35,6 +36,13 @@ def parse_args():
help="export table to google sheets",
action="store_true",
)
parser.add_argument(
'--start_cell',
type=str,
required=False,
help='Starting cell for Google Sheets export (e.g., "A1", "B3")',
default=None
)

parser.add_argument(
'--base_url',
Expand Down Expand Up @@ -162,14 +170,26 @@ def run(args, binded_repos, repos_for_wiki=None):
if args.wikis:
wikipars.wikiparser(repos_for_wiki, args.download_repos, args.out)
if args.export_google_sheets:
export_sheets.write_data_to_table(
args.out, args.google_token, args.table_id, args.sheet_id
)
if args.start_cell:
export_sheets.write_data_to_table(
args.out, args.google_token, args.table_id, args.sheet_id, args.start_cell
)
else:
export_sheets.write_data_to_table(
args.out, args.google_token, args.table_id, args.sheet_id
)


def main():
args = parse_args()

if args.start_cell is not None:
try:
args.start_cell = validate_and_normalize_cell(args.start_cell)
except ValueError as e:
print(f"Error in start_cell argument: {e}")
sys.exit(1)

if args.token:
tokens = [args.token]
else:
Expand Down
4 changes: 2 additions & 2 deletions src/export_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
INT_MASS = [{"one": 1, "two": 2, "what?": 3}]


def write_data_to_table(csv_path, google_token, table_id, sheet_id):
def write_data_to_table(csv_path, google_token, table_id, sheet_id, start_cell="A1"):
if google_token and sheet_id and table_id:
gc = pygsheets.authorize(service_file=google_token)
sh = gc.open_by_key(table_id)
Expand All @@ -25,4 +25,4 @@ def write_data_to_table(csv_path, google_token, table_id, sheet_id):
wk_content.clear()

# Запись новых данных
wk_content.set_dataframe(df, 'A1', copy_head=True)
wk_content.set_dataframe(df, start_cell, copy_head=True)
20 changes: 20 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime
from functools import wraps
import traceback
import re

import pytz

Expand Down Expand Up @@ -96,3 +97,22 @@ def wrapper(*args, **kwargs):
return default_return
return wrapper
return decorator


def validate_and_normalize_cell(cell: str):
"""
Проверка на соответствие строки формату ячейки Google Sheets:
- Идут буквы, потому идут цифры
- Буквы латинские
- Буквы в верхнем регистре (нижний регистр не вызывает ошибку -- принудительно переводится в верхний в return)
"""
if not re.match(r'^[A-Za-z]+\d+$', cell):
raise ValueError(f"Invalid cell format: '{cell}'. Must be letters followed by digits (e.g., 'A1', 'B3').")

letters = re.findall(r'[A-Za-z]+', cell)[0]
numbers = re.findall(r'\d+', cell)[0]

if not re.match(r'^[A-Za-z]+$', letters):
raise ValueError(f"Cell contains non-latin characters: '{letters}'. Only Latin letters allowed.")

return letters.upper() + numbers