11#!/usr/bin/env python3
22
3- """usage: comexdown { trade | table } <arguments>
3+ """Command-line interface for downloading Brazil's foreign trade data.
44
5- comexdown trade <year> ... -o <output>
5+ This module provides a CLI tool for downloading trade transaction data and
6+ auxiliary code tables from Brazil's Ministry of Economy (SECEX/COMEX).
67
7- comexdown table <table name> -o <output>
8+ Usage:
9+ comexdown { trade | table } <arguments>
810
11+ comexdown trade <year> ... -o <output>
12+ comexdown table <table name> -o <output>
13+
14+ Examples:
15+ Download exports and imports for 2020:
16+ $ comexdown trade 2020
17+
18+ Download exports for years 2018-2020:
19+ $ comexdown trade 2018:2020 -exp
20+
21+ Download municipality data:
22+ $ comexdown trade 2020 -mun
23+
24+ Download all auxiliary tables:
25+ $ comexdown table all
26+
27+ Download specific auxiliary table:
28+ $ comexdown table ncm
929"""
1030
1131
1737
1838
1939def expand_years (args_years : str ) -> list [int ]:
40+ """Expand year arguments into a list of individual years.
41+
42+ Processes command-line year arguments which may include individual years
43+ or year ranges (e.g., "2018:2020") and expands them into a complete list
44+ of years.
45+
46+ Parameters
47+ ----------
48+ args_years : str
49+ List of year strings, which can be individual years (e.g., "2020") or
50+ ranges (e.g., "2018:2020"). Ranges can be ascending or descending.
51+
52+ Returns
53+ -------
54+ list[int]
55+ Expanded list of years in the order they were specified.
56+
57+ Examples
58+ --------
59+ >>> expand_years(["2020"])
60+ [2020]
61+ >>> expand_years(["2018:2020"])
62+ [2018, 2019, 2020]
63+ >>> expand_years(["2020:2018"])
64+ [2020, 2019, 2018]
65+ >>> expand_years(["2018", "2020:2022"])
66+ [2018, 2020, 2021, 2022]
67+ """
2068 years = []
2169 for arg in args_years :
2270 if ":" in arg :
@@ -35,6 +83,30 @@ def expand_years(args_years: str) -> list[int]:
3583# ----------------------------TRANSACTION TRADE DATA---------------------------
3684# =============================================================================
3785def download_trade (args : argparse .Namespace ):
86+ """Download trade transaction data based on command-line arguments.
87+
88+ Handles the download of Brazil's foreign trade data for specified years.
89+ Supports downloading exports, imports, and municipality-level data for both
90+ modern NCM classification (1997+) and older NBM classification (1989-1996).
91+
92+ Parameters
93+ ----------
94+ args : argparse.Namespace
95+ Command-line arguments containing:
96+ - years: List of years or year ranges to download
97+ - exp: Flag to download export data only
98+ - imp: Flag to download import data only
99+ - mun: Flag to download municipality-level data
100+ - path: Output directory path for downloaded files
101+
102+ Notes
103+ -----
104+ - If neither exp nor imp flags are set, both are downloaded
105+ - Years before 1989 are not available
106+ - Years 1989-1996 use NBM classification (municipality data not available)
107+ - Years 1997+ use NCM classification
108+ - Special year value "complete" downloads complete historical datasets
109+ """
38110 if not args .exp and not args .imp :
39111 exp = imp = True
40112 else :
@@ -81,6 +153,25 @@ def download_trade(args: argparse.Namespace):
81153# ----------------------------AUXILIARY CODE TABLES----------------------------
82154# =============================================================================
83155def download_tables (args : argparse .Namespace ):
156+ """Download auxiliary code tables based on command-line arguments.
157+
158+ Handles downloading of various classification and reference tables used
159+ for interpreting Brazil's foreign trade data, including NCM codes, country
160+ codes, municipality codes, and various product classifications.
161+
162+ Parameters
163+ ----------
164+ args : argparse.Namespace
165+ Command-line arguments containing:
166+ - tables: List of table names to download (or 'all' for all tables)
167+ - path: Output directory path for downloaded tables
168+
169+ Notes
170+ -----
171+ - If no table names are provided, prints available tables and exits
172+ - Special value 'all' downloads all available auxiliary tables
173+ - Tables are saved in an 'auxiliary-tables' subdirectory
174+ """
84175 if args .tables == []:
85176 print_code_tables ()
86177 if "all" in args .tables :
@@ -98,6 +189,17 @@ def download_tables(args: argparse.Namespace):
98189
99190
100191def print_code_tables ():
192+ """Print formatted list of all available auxiliary code tables.
193+
194+ Displays a formatted list of all auxiliary tables available for download,
195+ including their short names, full names, and descriptions. Long descriptions
196+ are word-wrapped to fit within 70 characters per line.
197+
198+ Notes
199+ -----
200+ This function is called when the user runs 'comexdown table' without
201+ specifying any table names, serving as a help/reference guide.
202+ """
101203 print ("\n Available code tables:" )
102204 for table in TABLES :
103205 print (f"\n { table : <11} { TABLES [table ]['name' ]} " )
@@ -121,6 +223,13 @@ def print_code_tables():
121223
122224
123225def download_help (args : argparse .Namespace ):
226+ """Print CLI help message.
227+
228+ Parameters
229+ ----------
230+ args : argparse.Namespace
231+ Unused, but required by argparse callback interface.
232+ """
124233 print (__doc__ )
125234
126235
@@ -131,6 +240,28 @@ def set_download_trade_subparser(
131240 download_subs : argparse .ArgumentParser ,
132241 default_output : Path ,
133242):
243+ """Configure the 'trade' subcommand parser.
244+
245+ Sets up the argument parser for downloading trade transaction data,
246+ including all supported flags and options.
247+
248+ Parameters
249+ ----------
250+ download_subs : argparse.ArgumentParser
251+ Subparser collection to add the trade subcommand to.
252+ default_output : Path
253+ Default output directory path for downloaded files.
254+
255+ Notes
256+ -----
257+ Configures the following arguments:
258+ - years: Required positional argument for years to download
259+ - -exp: Optional flag to download only exports
260+ - -imp: Optional flag to download only imports
261+ - -mun: Optional flag to download municipality-level data
262+ - -nbm: Optional flag for NBM classification data
263+ - -o: Optional output path argument
264+ """
134265 # !!! DOWNLOAD TRADE TRANSACTIONS DATA
135266 download_trade_parser = download_subs .add_parser (
136267 "trade" , description = "Download Exports & Imports data" )
@@ -159,6 +290,23 @@ def set_download_table_subparser(
159290 download_subs : argparse .ArgumentParser ,
160291 default_output : Path ,
161292):
293+ """Configure the 'table' subcommand parser.
294+
295+ Sets up the argument parser for downloading auxiliary code tables.
296+
297+ Parameters
298+ ----------
299+ download_subs : argparse.ArgumentParser
300+ Subparser collection to add the table subcommand to.
301+ default_output : Path
302+ Default output directory path for downloaded tables.
303+
304+ Notes
305+ -----
306+ Configures the following arguments:
307+ - tables: Optional list of table names to download
308+ - -o: Optional output path argument
309+ """
162310 # !!! DOWNLOAD CODE TABLES
163311 download_table_parser = download_subs .add_parser (
164312 "table" , description = "Download code tables for Brazil's foreign data" )
@@ -184,6 +332,23 @@ def set_download_table_subparser(
184332
185333
186334def set_parser () -> argparse .ArgumentParser :
335+ """Create and configure the main argument parser.
336+
337+ Builds the complete command-line argument parser with all subcommands
338+ and their respective options.
339+
340+ Returns
341+ -------
342+ argparse.ArgumentParser
343+ Configured argument parser ready to parse command-line arguments.
344+
345+ Notes
346+ -----
347+ Sets up two subcommands:
348+ - trade: For downloading trade transaction data
349+ - table: For downloading auxiliary code tables
350+ Default output path is './data/secex-comex'
351+ """
187352 default_output = Path ("." , "data" , "secex-comex" )
188353
189354 parser = argparse .ArgumentParser (
@@ -199,6 +364,16 @@ def set_parser() -> argparse.ArgumentParser:
199364
200365
201366def main ():
367+ """Main entry point for the comexdown CLI tool.
368+
369+ Parses command-line arguments and dispatches to the appropriate
370+ download function based on the subcommand provided.
371+
372+ Notes
373+ -----
374+ This function is called when the module is executed as a script or
375+ when the 'comexdown' command is run from the command line.
376+ """
202377 parser = set_parser ()
203378 args = parser .parse_args ()
204379
0 commit comments