smartsheet-tools 0.0.1__py3-none-any.whl → 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+ from datetime import datetime
2
+ import re
3
+ from smartsheet.models import Cell, Row, Folder, Sheet
4
+
5
+ # Cache for column types to minimize API calls when correcting date formats
6
+ _COLUMN_TYPE_CACHE = {}
7
+
8
+ def norm(v):
9
+ if v is None:
10
+ return ""
11
+ s = str(v).strip().lower()
12
+ return re.sub(r"\.0+$", "", s)
13
+
14
+ def disp_or_val(cell):
15
+ # prefer display_value when Smartsheet provides a formatted value
16
+ dv = getattr(cell, "display_value", None)
17
+ return dv if dv not in (None, "") else cell.value
18
+
19
+ def title_to_index(sheet):
20
+ # authoritative positions from Smartsheet (not Python enumerate order)
21
+ return {c.title: c.index for c in sheet.columns}
22
+
23
+ def index_to_id(sheet):
24
+ # authoritative positions from Smartsheet (not Python enumerate order)
25
+ return {c.index: c.id for c in sheet.columns}
26
+
27
+ def id_to_index(sheet):
28
+ # authoritative positions from Smartsheet (not Python enumerate order)
29
+ return {c.id: c.index for c in sheet.columns}
30
+
31
+ def id_to_title(sheet):
32
+ return {c.id: c.title for c in sheet.columns}
33
+
34
+ def title_to_id(sheet):
35
+ return {c.title: c.id for c in sheet.columns}
36
+
37
+ def guard_row(row, *idxs):
38
+ # ensure row has enough cells for all requested positions
39
+ return max(idxs) < len(row.cells)
40
+
41
+ def datetime_to_isoformat(dt):
42
+ if dt is None:
43
+ return None
44
+ return dt.replace(microsecond=0).isoformat() + 'Z'
45
+
46
+ def standard_time_to_isoformat(st):
47
+ if st is None:
48
+ return None
49
+ return datetime_to_isoformat(datetime.strptime(st, "%m/%d/%Y"))
50
+
51
+ def get_cached_column_type(column_id, sheet_obj):
52
+ if sheet_obj.id not in _COLUMN_TYPE_CACHE:
53
+ _COLUMN_TYPE_CACHE[sheet_obj.id] = {}
54
+
55
+ if column_id not in _COLUMN_TYPE_CACHE[sheet_obj.id]:
56
+ _COLUMN_TYPE_CACHE[sheet_obj.id][column_id] = str(sheet_obj.get_column(column_id).type)
57
+
58
+ return _COLUMN_TYPE_CACHE[sheet_obj.id][column_id]
59
+
60
+ def get_col_names_of_date_cols(sheet_obj):
61
+ return [c.title for c in sheet_obj.columns if get_cached_column_type(c.id, sheet_obj) in ("DATE", "DATETIME")]
62
+
63
+ def brute_force_date_string(s, nonetype_if_fail=False):
64
+ # attempt to parse a date string in common formats to ISO 8601
65
+ if isinstance(s, datetime):
66
+ return datetime_to_isoformat(s)
67
+
68
+ if not isinstance(s, str):
69
+ return None if nonetype_if_fail else s
70
+
71
+ s = s.split(" ")[0]
72
+ for fmt in ("%Y-%m-%d", "%m/%d/%Y", "%m/%d/%y"):
73
+ try:
74
+ return datetime_to_isoformat(datetime.strptime(s, fmt))
75
+ except ValueError:
76
+ continue
77
+ return None if nonetype_if_fail else s
78
+
79
+
80
+ def is_date_col(column_id, sheet_obj):
81
+ column_type = get_cached_column_type(column_id, sheet_obj)
82
+ return column_type in ("DATE", "DATETIME")
83
+
84
+ def correct_date_format(isoformat_datetime, column_id, sheet_obj):
85
+ if isinstance(isoformat_datetime, datetime):
86
+ isoformat_datetime = datetime_to_isoformat(isoformat_datetime)
87
+
88
+ column_type = get_cached_column_type(column_id, sheet_obj)
89
+ if column_type == "DATE":
90
+ return isoformat_datetime.split("T",1)[0]
91
+ elif column_type == "DATETIME":
92
+ return isoformat_datetime
93
+ return None
94
+
95
+ def new_cell(column_id, value=None, strict=False, formula=None):
96
+ new_cell = Cell()
97
+ new_cell.column_id = column_id
98
+ if formula is not None:
99
+ new_cell.formula = formula
100
+ else:
101
+ new_cell.value = value
102
+ if strict:
103
+ new_cell.strict = True
104
+ return new_cell
105
+
106
+ def new_row(cells=None, id=None, parent_id=None, to_top=False):
107
+ new_row = Row()
108
+ if cells:
109
+ new_row.cells = cells
110
+ if id:
111
+ new_row.id = id
112
+ if parent_id:
113
+ new_row.parent_id = parent_id
114
+ if to_top:
115
+ new_row.to_top = to_top
116
+ return new_row
117
+
118
+ def walk_folder_for_sheets(smartsheet_client, folder_id):
119
+ for item in smartsheet_client.Folders.get_folder_children(folder_id).data:
120
+ if isinstance(item, Folder):
121
+ yield from walk_folder_for_sheets(smartsheet_client, item.id)
122
+ elif isinstance(item, Sheet):
123
+ yield item
124
+
125
+ def walk_workspace_for_sheets(smartsheet_client, workspace_id):
126
+ for item in smartsheet_client.Workspaces.get_workspace_children(workspace_id).data:
127
+ if isinstance(item, Folder):
128
+ yield from walk_folder_for_sheets(smartsheet_client, item.id)
129
+ elif isinstance(item, Sheet):
130
+ yield item
131
+
132
+ def walk_folder_for_folders(smartsheet_client, folder_id):
133
+ for item in smartsheet_client.Folders.get_folder_children(folder_id).data:
134
+ if isinstance(item, Folder):
135
+ yield item
136
+ yield from walk_folder_for_folders(smartsheet_client, item.id)
137
+
138
+ def walk_workspace_for_folders(smartsheet_client, workspace_id):
139
+ for item in smartsheet_client.Workspaces.get_workspace_children(workspace_id).data:
140
+ if isinstance(item, Folder):
141
+ yield item
142
+ yield from walk_folder_for_folders(smartsheet_client, item.id)
143
+
144
+ def walk_sheet_names_from_workspace(smartsheet_client, workspace_id):
145
+ for sheet in walk_workspace_for_sheets(smartsheet_client, workspace_id):
146
+ yield sheet.name
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: smartsheet_tools
3
- Version: 0.0.1
3
+ Version: 0.0.3
4
4
  Summary: A collection of convenience functions to aid with transitioning from simple-smartsheet to the SDK API and common tasks
5
5
  Author: Ashton Pooley
6
6
  Author-email: Ashton Pooley <ashton@ashi.digital>
@@ -0,0 +1,6 @@
1
+ smartsheet_tools/__init__.py,sha256=-0zRzn_7MUDL4sFfNzXRWTJj0MUY7NZ96O_5rSTmJiE,5268
2
+ smartsheet_tools-0.0.3.dist-info/licenses/LICENSE,sha256=xshMXNQ83e1x1bG3-9fQ5U8hnMaJsv79ke3xuKmI2PI,31914
3
+ smartsheet_tools-0.0.3.dist-info/METADATA,sha256=X_Tkf-hEspLuLBHreCq65BOSeKaBKi258LkNiL8sab8,834
4
+ smartsheet_tools-0.0.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
+ smartsheet_tools-0.0.3.dist-info/top_level.txt,sha256=UXKUTK6mn1resx7hDN-MqSLi6ZnojUbkJ44VzmNmYi8,17
6
+ smartsheet_tools-0.0.3.dist-info/RECORD,,
@@ -0,0 +1 @@
1
+ smartsheet_tools
@@ -1,5 +0,0 @@
1
- smartsheet_tools-0.0.1.dist-info/licenses/LICENSE,sha256=xshMXNQ83e1x1bG3-9fQ5U8hnMaJsv79ke3xuKmI2PI,31914
2
- smartsheet_tools-0.0.1.dist-info/METADATA,sha256=0JXbYO3MWkrZl9TXDer_AM1oSjbapvyvYxzR_383n90,834
3
- smartsheet_tools-0.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
4
- smartsheet_tools-0.0.1.dist-info/top_level.txt,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
5
- smartsheet_tools-0.0.1.dist-info/RECORD,,