smartsheet-tools 0.0.2__py3-none-any.whl → 0.0.4__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.
@@ -1,5 +1,12 @@
1
+ from datetime import datetime
1
2
  import re
2
- from smartsheet.models import Cell, Row
3
+ from smartsheet.models import Cell, Row, Folder, Sheet
4
+ from smartsheet.models import Column
5
+
6
+ # Cache for column types to minimize API calls when correcting date formats
7
+ _COLUMN_TYPE_CACHE = {}
8
+ _TITLE_TO_ID_CACHE = {}
9
+ _ID_TO_INDEX_CACHE = {}
3
10
 
4
11
  def norm(v):
5
12
  if v is None:
@@ -14,11 +21,15 @@ def disp_or_val(cell):
14
21
 
15
22
  def title_to_index(sheet):
16
23
  # authoritative positions from Smartsheet (not Python enumerate order)
17
- return {c.title: c.index for c in sheet.columns}
24
+ if sheet.id not in _TITLE_TO_ID_CACHE:
25
+ _TITLE_TO_ID_CACHE[sheet.id] = {c.title: c.index for c in sheet.columns}
26
+ return _TITLE_TO_ID_CACHE[sheet.id]
18
27
 
19
28
  def index_to_id(sheet):
20
29
  # authoritative positions from Smartsheet (not Python enumerate order)
21
- return {c.index: c.id for c in sheet.columns}
30
+ if sheet.id not in _ID_TO_INDEX_CACHE:
31
+ _ID_TO_INDEX_CACHE[sheet.id] = {c.index: c.id for c in sheet.columns}
32
+ return _ID_TO_INDEX_CACHE[sheet.id]
22
33
 
23
34
  def id_to_index(sheet):
24
35
  # authoritative positions from Smartsheet (not Python enumerate order)
@@ -34,9 +45,64 @@ def guard_row(row, *idxs):
34
45
  # ensure row has enough cells for all requested positions
35
46
  return max(idxs) < len(row.cells)
36
47
 
37
- def new_cell(column_id, value=None, strict=False, formula=None):
48
+ def datetime_to_isoformat(dt):
49
+ if dt is None:
50
+ return None
51
+ return dt.replace(microsecond=0).isoformat() + 'Z'
52
+
53
+ def standard_time_to_isoformat(st):
54
+ if st is None:
55
+ return None
56
+ return datetime_to_isoformat(datetime.strptime(st, "%m/%d/%Y"))
57
+
58
+ def get_cached_column_type(column_id, sheet_obj):
59
+ if sheet_obj.id not in _COLUMN_TYPE_CACHE:
60
+ _COLUMN_TYPE_CACHE[sheet_obj.id] = {}
61
+
62
+ if column_id not in _COLUMN_TYPE_CACHE[sheet_obj.id]:
63
+ _COLUMN_TYPE_CACHE[sheet_obj.id][column_id] = str(sheet_obj.get_column(column_id).type)
64
+
65
+ return _COLUMN_TYPE_CACHE[sheet_obj.id][column_id]
66
+
67
+ def get_col_names_of_date_cols(sheet_obj):
68
+ return [c.title for c in sheet_obj.columns if get_cached_column_type(c.id, sheet_obj) in ("DATE", "DATETIME")]
69
+
70
+ def brute_force_date_string(s, nonetype_if_fail=False):
71
+ # attempt to parse a date string in common formats to ISO 8601
72
+ if isinstance(s, datetime):
73
+ return datetime_to_isoformat(s)
74
+
75
+ if not isinstance(s, str):
76
+ return None if nonetype_if_fail else s
77
+
78
+ s = s.split(" ")[0]
79
+ for fmt in ("%Y-%m-%d", "%m/%d/%Y", "%m/%d/%y"):
80
+ try:
81
+ return datetime_to_isoformat(datetime.strptime(s, fmt))
82
+ except ValueError:
83
+ continue
84
+ return None if nonetype_if_fail else s
85
+
86
+
87
+ def is_date_col(column_id, sheet_obj):
88
+ column_type = get_cached_column_type(column_id, sheet_obj)
89
+ return column_type in ("DATE", "DATETIME")
90
+
91
+ def correct_date_format(isoformat_datetime, column_id, sheet_obj):
92
+ if isinstance(isoformat_datetime, datetime):
93
+ isoformat_datetime = datetime_to_isoformat(isoformat_datetime)
94
+
95
+ column_type = get_cached_column_type(column_id, sheet_obj)
96
+ if column_type == "DATE":
97
+ return isoformat_datetime.split("T",1)[0]
98
+ elif column_type == "DATETIME":
99
+ return isoformat_datetime
100
+ return None
101
+
102
+ def new_cell(column_id=None, value=None, strict=False, formula=None):
38
103
  new_cell = Cell()
39
- new_cell.column_id = column_id
104
+ if column_id is not None:
105
+ new_cell.column_id = column_id
40
106
  if formula is not None:
41
107
  new_cell.formula = formula
42
108
  else:
@@ -45,12 +111,67 @@ def new_cell(column_id, value=None, strict=False, formula=None):
45
111
  new_cell.strict = True
46
112
  return new_cell
47
113
 
48
- def new_row(cells=None, parent_id=None, to_top=False):
114
+ def new_row(cells=None, id=None, parent_id=None, to_top=False, locked=False):
49
115
  new_row = Row()
50
116
  if cells:
51
117
  new_row.cells = cells
118
+ if id:
119
+ new_row.id = id
52
120
  if parent_id:
53
121
  new_row.parent_id = parent_id
54
122
  if to_top:
55
123
  new_row.to_top = to_top
56
- return new_row
124
+ if locked:
125
+ new_row.locked = locked
126
+ return new_row
127
+
128
+ def walk_folder_for_sheets(smartsheet_client, folder_id):
129
+ for item in smartsheet_client.Folders.get_folder_children(folder_id).data:
130
+ if isinstance(item, Folder):
131
+ yield from walk_folder_for_sheets(smartsheet_client, item.id)
132
+ elif isinstance(item, Sheet):
133
+ yield item
134
+
135
+ def walk_workspace_for_sheets(smartsheet_client, workspace_id):
136
+ for item in smartsheet_client.Workspaces.get_workspace_children(workspace_id).data:
137
+ if isinstance(item, Folder):
138
+ yield from walk_folder_for_sheets(smartsheet_client, item.id)
139
+ elif isinstance(item, Sheet):
140
+ yield item
141
+
142
+ def walk_folder_for_folders(smartsheet_client, folder_id):
143
+ for item in smartsheet_client.Folders.get_folder_children(folder_id).data:
144
+ if isinstance(item, Folder):
145
+ yield item
146
+ yield from walk_folder_for_folders(smartsheet_client, item.id)
147
+
148
+ def walk_workspace_for_folders(smartsheet_client, workspace_id):
149
+ for item in smartsheet_client.Workspaces.get_workspace_children(workspace_id).data:
150
+ if isinstance(item, Folder):
151
+ yield item
152
+ yield from walk_folder_for_folders(smartsheet_client, item.id)
153
+
154
+ def walk_sheet_names_from_workspace(smartsheet_client, workspace_id):
155
+ for sheet in walk_workspace_for_sheets(smartsheet_client, workspace_id):
156
+ yield sheet.name
157
+
158
+ def new_column(column_type, title, index=None, id=None, options=None, symbol=None, primary=False, hidden=False, locked=False):
159
+ new_column = Column()
160
+
161
+ new_column.type = column_type
162
+ new_column.title = title
163
+ if index is not None:
164
+ new_column.index = index
165
+ if id is not None:
166
+ new_column.id = id
167
+ if options is not None and column_type in ("PICKLIST", "MULTI_PICKLIST"):
168
+ new_column.options = options
169
+ if symbol is not None and column_type == "CHECKBOX":
170
+ new_column.symbol = symbol
171
+ if primary:
172
+ new_column.primary = True
173
+ if hidden:
174
+ new_column.hidden = True
175
+ if locked:
176
+ new_column.locked = True
177
+ return new_column
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: smartsheet_tools
3
- Version: 0.0.2
3
+ Version: 0.0.4
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=CENQb8WEHEa1pPCISbdBRQyZbJWmv9IIUFrJuaeAP4w,6420
2
+ smartsheet_tools-0.0.4.dist-info/licenses/LICENSE,sha256=xshMXNQ83e1x1bG3-9fQ5U8hnMaJsv79ke3xuKmI2PI,31914
3
+ smartsheet_tools-0.0.4.dist-info/METADATA,sha256=8QOUTdU3lqlThmuzOvNOMxw1PAXb2EhzJ7z0dbLkk50,834
4
+ smartsheet_tools-0.0.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
+ smartsheet_tools-0.0.4.dist-info/top_level.txt,sha256=UXKUTK6mn1resx7hDN-MqSLi6ZnojUbkJ44VzmNmYi8,17
6
+ smartsheet_tools-0.0.4.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- smartsheet_tools/__init__.py,sha256=LVXn-fARI98McLcctaxpF0SD7_RCh6Uu6RVxJmzMMTE,1683
2
- smartsheet_tools-0.0.2.dist-info/licenses/LICENSE,sha256=xshMXNQ83e1x1bG3-9fQ5U8hnMaJsv79ke3xuKmI2PI,31914
3
- smartsheet_tools-0.0.2.dist-info/METADATA,sha256=yD3p6zD6w9xi1oKAA6bszAzhThot4EOa_2aHnikX4sw,834
4
- smartsheet_tools-0.0.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
- smartsheet_tools-0.0.2.dist-info/top_level.txt,sha256=UXKUTK6mn1resx7hDN-MqSLi6ZnojUbkJ44VzmNmYi8,17
6
- smartsheet_tools-0.0.2.dist-info/RECORD,,