Skip to content

Commit a3bf5a0

Browse files
authored
Split Excel_Format into 3 constructors (#9308)
Splitting the `Excel_Format` into 3 constructors.
1 parent ed46fdc commit a3bf5a0

File tree

21 files changed

+367
-268
lines changed

21 files changed

+367
-268
lines changed

distribution/lib/Standard/Base/0.0.0-dev/src/Data.enso

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ from project.System.File_Format import Auto_Detect, File_Format
5757
Read the sheet named `Dates` from an XLS and convert it to a table.
5858

5959
from Standard.Table import all
60-
from Standard.Table import Excel, Worksheet
6160
import Standard.Examples
6261

63-
example_xls_to_table = Data.read Examples.xls (Excel (Worksheet 'Dates'))
62+
example_xls_to_table = Data.read Examples.xls (Excel_Format.Sheet 'Dates')
6463
@path Text_Input
6564
@format File_Format.default_widget
6665
read : Text | File -> File_Format -> Problem_Behavior -> Any ! File_Error

distribution/lib/Standard/Base/0.0.0-dev/src/Data/XML/XML_Format.enso

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import project.Any.Any
22
import project.Data.Text.Text
3+
import project.Data.Vector.Vector
34
import project.Data.XML.XML_Document
45
import project.Errors.Problem_Behavior.Problem_Behavior
6+
import project.Meta
57
import project.Network.URI.URI
68
import project.Nothing.Nothing
79
import project.System.File.File
810
import project.System.File.Generic.Writable_File.Writable_File
911
import project.System.File_Format_Metadata.File_Format_Metadata
1012
import project.System.Input_Stream.Input_Stream
1113
from project.Data.Text.Extensions import all
14+
from project.Metadata.Choice import Option
1215

1316
## A `File_Format` for reading and writing XML files.
1417
type XML_Format
@@ -35,6 +38,11 @@ type XML_Format
3538
_ = [file]
3639
Nothing
3740

41+
42+
## PRIVATE
43+
get_dropdown_options : Vector Option
44+
get_dropdown_options = [Option "XML" (Meta.get_qualified_type_name XML_Format)]
45+
3846
## PRIVATE
3947
Implements the `File.read` for this `File_Format`
4048
read : File -> Problem_Behavior -> Any

distribution/lib/Standard/Base/0.0.0-dev/src/Enso_Cloud/Data_Link.enso

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type Data_Link_Format
3838
_ = file
3939
Nothing
4040

41+
## PRIVATE
42+
We currently don't display data-link as a specific format.
43+
It relies on `Auto_Detect`.
44+
get_dropdown_options = []
45+
4146
## PRIVATE
4247
Implements the `File.read` for this `File_Format`
4348
read : File -> Problem_Behavior -> Any

distribution/lib/Standard/Base/0.0.0-dev/src/System/File.enso

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,9 @@ type File
277277
Read the sheet named `Dates` from an XLS and convert it to a table.
278278

279279
from Standard.Table import all
280-
from Standard.Table import Excel, Worksheet
281280
import Standard.Examples
282281

283-
example_xls_to_table = Examples.xls.read (Excel (Worksheet 'Dates'))
282+
example_xls_to_table = Examples.xls.read (Excel_Format.Sheet 'Dates')
284283
@format File_Format.default_widget
285284
read : File_Format -> Problem_Behavior -> Any ! File_Error
286285
read self format=Auto_Detect (on_problems=Problem_Behavior.Report_Warning) =

distribution/lib/Standard/Base/0.0.0-dev/src/System/File_Format.enso

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,12 @@ type File_Format
8989
_ = [file, on_problems]
9090
Unimplemented.throw "This is an interface only."
9191

92-
## PRIVATE
93-
Create the constructor code for a File_Format type.
94-
constructor_code : Any -> Text
95-
constructor_code type_obj =
96-
## Workaround for JSON and Auto_Detect
97-
case type_obj of
98-
JSON_Format -> "JSON_Format"
99-
Auto_Detect -> "Auto_Detect"
100-
_ ->
101-
type_name = Meta.get_qualified_type_name type_obj
102-
ctors = Meta.meta type_obj . constructors
103-
is_singleton_type = ctors.length == 0
104-
if is_singleton_type then type_name else
105-
type_name + "." + ctors.first.name
106-
10792
## PRIVATE
10893
default_widget : Widget
10994
default_widget =
110-
all_types = [Auto_Detect] + format_types
111-
make_name type_obj = type_obj.to_text.replace "_Format" "" . replace "_" " "
112-
Single_Choice display=Display.Always values=(all_types.map n->(Option (make_name n) (File_Format.constructor_code n)))
95+
options = [Option "Auto Detect" "Auto_Detect"] +
96+
format_types.flat_map .get_dropdown_options
97+
Single_Choice display=Display.Always values=options
11398

11499
## A file format for plain text files.
115100
type Plain_Text_Format
@@ -144,6 +129,10 @@ type Plain_Text_Format
144129
for_file_write : Writable_File -> Plain_Text_Format | Nothing
145130
for_file_write file = Plain_Text_Format.for_read file
146131

132+
## PRIVATE
133+
get_dropdown_options : Vector Option
134+
get_dropdown_options = [Option "Plain Text" (Meta.get_qualified_type_name Plain_Text_Format)+".Plain_Text"]
135+
147136
## PRIVATE
148137
Implements the `File.read` for this `File_Format`
149138
read : File -> Problem_Behavior -> Any
@@ -179,6 +168,10 @@ type Bytes
179168
for_file_write : Writable_File -> Bytes | Nothing
180169
for_file_write file = Bytes.for_read file
181170

171+
## PRIVATE
172+
get_dropdown_options : Vector Option
173+
get_dropdown_options = [Option "Bytes" (Meta.get_qualified_type_name Bytes)]
174+
182175
## PRIVATE
183176
Implements the `File.read` for this `File_Format`
184177
read : File -> Problem_Behavior -> Any
@@ -214,6 +207,10 @@ type JSON_Format
214207
for_file_write : Writable_File -> JSON_Format | Nothing
215208
for_file_write file = JSON_Format.for_read file
216209

210+
## PRIVATE
211+
get_dropdown_options : Vector Option
212+
get_dropdown_options = [Option "JSON" (Meta.get_qualified_type_name JSON_Format)]
213+
217214
## PRIVATE
218215
Implements the `File.read` for this `File_Format`
219216
read : File -> Problem_Behavior -> Any

distribution/lib/Standard/Database/0.0.0-dev/src/Connection/SQLite_Format.enso

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from Standard.Base import all
22
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
33
import Standard.Base.System.File.Generic.Writable_File.Writable_File
44
import Standard.Base.System.File_Format_Metadata.File_Format_Metadata
5+
from Standard.Base.Metadata.Choice import Option
56

67
import project.Connection.Database
78
import project.Connection.SQLite_Details.SQLite_Details
@@ -36,6 +37,10 @@ type SQLite_Format
3637
".sqlite" -> SQLite_Format.For_File
3738
_ -> Nothing
3839

40+
## PRIVATE
41+
get_dropdown_options : Vector Option
42+
get_dropdown_options = [Option "SQLite" (Meta.get_qualified_type_name SQLite_Format)+".For_File"]
43+
3944
## PRIVATE
4045
Implements the `File.read` for this `File_Format`
4146
read : File -> Problem_Behavior -> Any

distribution/lib/Standard/Image/0.0.0-dev/src/Image_File_Format.enso

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from Standard.Base import all
22
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
33
import Standard.Base.System.File.Generic.Writable_File.Writable_File
44
import Standard.Base.System.File_Format_Metadata.File_Format_Metadata
5+
from Standard.Base.Metadata.Choice import Option
56

67
import project.Image.Image
78

@@ -26,6 +27,10 @@ type Image_File_Format
2627
for_file_write : Writable_File -> Image_File_Format | Nothing
2728
for_file_write file = Image_File_Format.for_read file
2829

30+
## PRIVATE
31+
get_dropdown_options : Vector Option
32+
get_dropdown_options = [Option "Image" (Meta.get_qualified_type_name Image_File_Format)+".For_File"]
33+
2934
## PRIVATE
3035
Implements the `File.read` for this `File_Format`
3136
read : File -> Problem_Behavior -> Any

distribution/lib/Standard/Table/0.0.0-dev/src/Data/Table.enso

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,9 +2646,9 @@ type Table
26462646
Write a table to an XLSX file.
26472647

26482648
import Standard.Examples
2649-
from Standard.Table import Excel
2649+
from Standard.Table import all
26502650

2651-
example_to_xlsx = Examples.inventory_table.write (enso_project.data / "example_xlsx_output.xlsx") Excel
2651+
example_to_xlsx = Examples.inventory_table.write (enso_project.data / "example_xlsx_output.xlsx") (Excel_Format.Sheet "MySheetName")
26522652
@format Widget_Helpers.write_table_selector
26532653
write : Writable_File -> File_Format -> Existing_File_Behavior -> Match_Columns -> Problem_Behavior -> File ! Column_Count_Mismatch | Illegal_Argument | File_Error
26542654
write self path:Writable_File format=Auto_Detect on_existing_file=Existing_File_Behavior.Backup match_columns=Match_Columns.By_Name on_problems=Report_Warning =

distribution/lib/Standard/Table/0.0.0-dev/src/Delimited/Delimited_Format.enso

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Standard.Base.Network.HTTP.Response.Response
44
import Standard.Base.System.File.Generic.Writable_File.Writable_File
55
import Standard.Base.System.File_Format_Metadata.File_Format_Metadata
66
import Standard.Base.System.Input_Stream.Input_Stream
7+
from Standard.Base.Metadata.Choice import Option
78
from Standard.Base.Widget_Helpers import make_file_read_delimiter_selector
89

910
import project.Data.Data_Formatter.Data_Formatter
@@ -82,6 +83,10 @@ type Delimited_Format
8283
for_file_write : Writable_File -> Delimited_Format | Nothing
8384
for_file_write file = Delimited_Format.for_read file
8485

86+
## PRIVATE
87+
get_dropdown_options : Vector Option
88+
get_dropdown_options = [Option "Delimited" (Meta.get_qualified_type_name Delimited_Format)+".Delimited"]
89+
8590
## PRIVATE
8691
ADVANCED
8792
Implements the `File.read` for this `File_Format`

distribution/lib/Standard/Table/0.0.0-dev/src/Excel/Excel_Format.enso

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from Standard.Base import all
22
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
3+
import Standard.Base.Metadata.Display
34
import Standard.Base.System.File.Generic.Writable_File.Writable_File
45
import Standard.Base.System.File_Format_Metadata.File_Format_Metadata
56
import Standard.Base.System.Input_Stream.Input_Stream
7+
from Standard.Base.Metadata.Choice import Option
8+
from Standard.Base.Metadata.Widget import Text_Input, Numeric_Input
69

710
import project.Data.Match_Columns.Match_Columns
811
import project.Data.Table.Table
9-
import project.Excel.Excel_Section.Excel_Section
12+
import project.Excel.Excel_Range.Excel_Range
13+
import project.Internal.Excel_Section.Excel_Section
1014
import project.Excel.Excel_Workbook.Excel_Workbook
1115
import project.Internal.Excel_Reader
1216
import project.Internal.Excel_Writer
@@ -22,52 +26,87 @@ should_treat_as_xls_format xls_format file =
2226

2327
## Read the file to a `Table` from an Excel file
2428
type Excel_Format
25-
## Read Excels files into a Table or Vector.
29+
## Reads an Excel file as a connection to an `Excel_Workbook`.
2630

2731
Arguments:
28-
- section: The `Excel_Section` to read from the workbook.
29-
This can be one of:
30-
- `Workbook` - open the workbook as a connection.
31-
- `Sheet_Names` - outputs a `Vector` of sheet names.
32-
- `Range_Names` - outputs a `Vector` of range names.
33-
- `Worksheet` - outputs a `Table` containing the specified sheet.
34-
- `Cell_Range` - outputs a `Table` containing the specified range.
32+
- xls_format:
33+
If set to `True`, the file is read as an Excel 95-2003 format.
34+
If set to `False`, the file is read as an Excel 2007+ format.
35+
`Infer` will attempt to deduce this from the extension of the filename.
36+
- default_sheet: The default sheet to use when writing a table to a file
37+
with these format settings.
38+
@default_sheet Text_Input
39+
Workbook (xls_format : Boolean | Infer = Infer) (default_sheet : Text = "EnsoSheet")
40+
41+
## Reads a sheet from an Excel file as a `Table`.
42+
43+
Arguments:
44+
- sheet: The sheet number or name.
45+
- headers: If set to `True`, the first row is used as column names. If
46+
set to `False`, the column names are Excel column names. If set to
47+
`Infer`, the process tries to infer if headers are present on the first
48+
row. If the column names are not unique, numeric suffixes will be
49+
appended to disambiguate them.
50+
- skip_rows: The number of rows to skip before reading the data.
51+
- row_limit: The maximum number of rows to read. If set to `Nothing`, all
52+
rows are read.
53+
- xls_format:
54+
If set to `True`, the file is read as an Excel 95-2003 format.
55+
If set to `False`, the file is read as an Excel 2007+ format.
56+
`Infer` will attempt to deduce this from the extension of the filename.
57+
@sheet (Text_Input display=Display.Always)
58+
Sheet (sheet:(Integer|Text)=1) (headers:(Boolean|Infer)=Infer) (skip_rows:Integer=0) (row_limit:(Integer|Nothing)=Nothing) (xls_format:Boolean|Infer=Infer)
59+
60+
## Reads a range from an Excel file as a `Table`.
61+
62+
Arguments:
63+
- address: A name of a range or an Excel-style address (e.g. Sheet1!A1:B2).
3564
- headers: If set to `True`, the first row is used as column names. If
3665
set to `False`, the column names are Excel column names. If set to
3766
`Infer`, the process tries to infer if headers are present on the first
3867
row. If the column names are not unique, numeric suffixes will be
3968
appended to disambiguate them.
69+
- skip_rows: The number of rows to skip before reading the data.
70+
- row_limit: The maximum number of rows to read. If set to `Nothing`, all
71+
rows are read.
4072
- xls_format:
4173
If set to `True`, the file is read as an Excel 95-2003 format.
4274
If set to `False`, the file is read as an Excel 2007+ format.
4375
`Infer` will attempt to deduce this from the extension of the filename.
44-
- default_sheet: The default sheet to use if `section` is set to
45-
`Excel_Section.Workbook`.
46-
Excel (section:Excel_Section=Excel_Section.Workbook) (headers:(Boolean|Infer)=Infer) (xls_format:(Boolean|Infer)=Infer) (default_sheet:Text="EnsoSheet")
76+
@address Text_Input
77+
Range (address:(Text|Excel_Range)) (headers:(Boolean|Infer)=Infer) (skip_rows:Integer=0) (row_limit:(Integer|Nothing)=Nothing) (xls_format : Boolean | Infer = Infer)
4778

4879
## PRIVATE
4980
ADVANCED
5081
If the File_Format supports reading from the file, return a configured instance.
5182
for_read : File_Format_Metadata -> Excel_Format | Nothing
5283
for_read file:File_Format_Metadata =
5384
is_xls = xls_format_from_metadata file
54-
is_xls.if_not_nothing <|
55-
Excel_Format.Excel xls_format=is_xls
85+
is_xls.if_not_nothing <| Excel_Format.Workbook xls_format=is_xls
5686

5787
## PRIVATE
5888
If this File_Format should be used for writing to that file, return a configured instance.
5989
for_file_write : Writable_File -> Excel_Format | Nothing
6090
for_file_write file = Excel_Format.for_read file
6191

92+
## PRIVATE
93+
get_dropdown_options : Vector Option
94+
get_dropdown_options =
95+
fqn = Meta.get_qualified_type_name Excel_Format
96+
workbook = Option "Excel Workbook" fqn+".Workbook"
97+
sheet = Option "Excel Sheet" fqn+".Sheet"
98+
range = Option "Excel Range" fqn+".Range"
99+
[workbook, sheet, range]
100+
62101
## PRIVATE
63102
ADVANCED
64103
Implements the `File.read` for this `File_Format`
65104
read : File -> Problem_Behavior -> Any
66105
read self file on_problems =
67106
format = should_treat_as_xls_format self.xls_format file
68-
case self.section of
69-
Excel_Section.Workbook -> Excel_Workbook.new file format self.headers
70-
_ -> Excel_Reader.read_file file self.section self.headers on_problems format
107+
case self of
108+
Excel_Format.Workbook _ _ -> Excel_Workbook.new file format
109+
_ -> Excel_Reader.read_file file (as_section self) on_problems format
71110

72111
## PRIVATE
73112
Implements decoding the format from a stream.
@@ -88,8 +127,10 @@ type Excel_Format
88127
Error.throw (Illegal_Argument.Error message)
89128

90129
Excel_Reader.handle_bad_format_with_handler bad_format <|
91-
workbook = Excel_Workbook.from_stream stream xls_format self.headers
92-
workbook.read_section self.section
130+
workbook = Excel_Workbook.from_stream stream xls_format
131+
case self of
132+
Excel_Format.Workbook _ _ -> workbook
133+
_ -> workbook.read_section (as_section self)
93134

94135
## PRIVATE
95136
ADVANCED
@@ -114,13 +155,7 @@ type Excel_Format
114155
write_table : File -> Table -> Existing_File_Behavior -> Match_Columns -> Problem_Behavior -> File
115156
write_table self file table on_existing_file match_columns on_problems =
116157
format = should_treat_as_xls_format self.xls_format file
117-
118-
case self.section of
119-
Excel_Section.Sheet_Names -> Error.throw (Illegal_Argument.Error "Sheet_Names cannot be used for `write`.")
120-
Excel_Section.Range_Names -> Error.throw (Illegal_Argument.Error "Range_Names cannot be used for `write`.")
121-
Excel_Section.Workbook ->
122-
Excel_Writer.write_file file table on_existing_file (Excel_Section.Worksheet self.default_sheet) True match_columns on_problems format
123-
_ -> Excel_Writer.write_file file table on_existing_file self.section self.headers match_columns on_problems format
158+
Excel_Writer.write_file file table on_existing_file (as_section self) match_columns on_problems format
124159

125160
## PRIVATE
126161
This function returns:
@@ -142,3 +177,15 @@ xls_format_from_metadata (metadata : File_Format_Metadata) =
142177
".xls" -> True
143178
".xlt" -> True
144179
_ -> Nothing
180+
181+
## PRIVATE
182+
Converts this format to a corresponding `Excel_Section`.
183+
as_section (format : Excel_Format) -> Excel_Section = case format of
184+
Excel_Format.Workbook _ default_sheet ->
185+
## This case is only used for when writing to the workbook.
186+
This results in writing to the default sheet, and we always write headers (ignored with append).
187+
Excel_Section.Worksheet default_sheet headers=True
188+
Excel_Format.Sheet sheet headers skip_rows row_limit _ ->
189+
Excel_Section.Worksheet sheet headers skip_rows row_limit
190+
Excel_Format.Range address headers skip_rows row_limit _ ->
191+
Excel_Section.Cell_Range address headers skip_rows row_limit

0 commit comments

Comments
 (0)