smartsheet-tools 0.0.2__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.
@@ -1,5 +1,9 @@
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
+
5
+ # Cache for column types to minimize API calls when correcting date formats
6
+ _COLUMN_TYPE_CACHE = {}
3
7
 
4
8
  def norm(v):
5
9
  if v is None:
@@ -34,6 +38,60 @@ def guard_row(row, *idxs):
34
38
  # ensure row has enough cells for all requested positions
35
39
  return max(idxs) < len(row.cells)
36
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
+
37
95
  def new_cell(column_id, value=None, strict=False, formula=None):
38
96
  new_cell = Cell()
39
97
  new_cell.column_id = column_id
@@ -45,12 +103,44 @@ def new_cell(column_id, value=None, strict=False, formula=None):
45
103
  new_cell.strict = True
46
104
  return new_cell
47
105
 
48
- def new_row(cells=None, parent_id=None, to_top=False):
106
+ def new_row(cells=None, id=None, parent_id=None, to_top=False):
49
107
  new_row = Row()
50
108
  if cells:
51
109
  new_row.cells = cells
110
+ if id:
111
+ new_row.id = id
52
112
  if parent_id:
53
113
  new_row.parent_id = parent_id
54
114
  if to_top:
55
115
  new_row.to_top = to_top
56
- return new_row
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.2
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,,
@@ -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,,