2222- csv
2323"""
2424from __future__ import annotations
25+ from collections import OrderedDict
2526
2627import csv
2728import json
@@ -56,13 +57,14 @@ def stripAnsi(string: str) -> str:
5657 return re .compile (r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])" ).sub ("" , string )
5758
5859
59- def ansi (myLice : License , packages : list [PackageInfo ]) -> str :
60+ def ansi (myLice : License , packages : list [PackageInfo ], hide_parameters : list [ str ] = [] ) -> str :
6061 """Format to ansi.
6162
6263 Args:
6364 ----
6465 myLice (License): project license
6566 packages (list[PackageInfo]): list of PackageCompats to format.
67+ hide_parameters (list[str]): list of parameters to ignore in the output.
6668
6769 Returns:
6870 -------
@@ -92,41 +94,55 @@ def ansi(myLice: License, packages: list[PackageInfo]) -> str:
9294 console .print (table )
9395
9496 table = Table (title = "\n List Of Packages" )
95- table .add_column ("Compatible" , style = "cyan" )
96- table .add_column ("Package" , style = "magenta" )
97- table .add_column ("License(s)" , style = "magenta" )
97+ if licensecompat_bool := not "LICENSECOMPAT" in hide_parameters :
98+ table .add_column ("Compatible" , style = "cyan" )
99+ if name_bool := not "NAME" in hide_parameters :
100+ table .add_column ("Package" , style = "magenta" )
101+ if license_bool := not "LICENSE" in hide_parameters :
102+ table .add_column ("License(s)" , style = "magenta" )
98103 licenseCompat = (
99104 "[red]✖[/]" ,
100105 "[green]✔[/]" ,
101106 )
102- _ = [table .add_row (licenseCompat [x .licenseCompat ], x .name , x .license ) for x in packages ]
107+ _ = [
108+ table .add_row (
109+ * (
110+ ([licenseCompat [x .licenseCompat ]] if licensecompat_bool else []) +
111+ ([x .name ] if name_bool else []) +
112+ ([x .license ] if license_bool else [])
113+ )
114+ )
115+ for x in packages
116+ ]
103117 console .print (table )
104118 return string .getvalue ()
105119
106120
107- def plainText (myLice : License , packages : list [PackageInfo ]) -> str :
121+ def plainText (myLice : License , packages : list [PackageInfo ], hide_parameters : list [ str ] = [] ) -> str :
108122 """Format to ansi.
109123
110124 Args:
111125 ----
112126 myLice (License): project license
113127 packages (list[PackageInfo]): list of PackageCompats to format.
128+ hide_parameters (list[str]): list of parameters to ignore in the output.
114129
115130 Returns:
116131 -------
117132 str: string to send to specified output in plain text format
118133
119134 """
120- return stripAnsi (ansi (myLice , packages ))
135+ return stripAnsi (ansi (myLice , packages , hide_parameters ))
121136
122137
123- def markdown (myLice : License , packages : list [PackageInfo ]) -> str :
138+ def markdown (myLice : License , packages : list [PackageInfo ], hide_parameters : list [ str ] = [] ) -> str :
124139 """Format to markdown.
125140
126141 Args:
127142 ----
128143 myLice (License): project license
129144 packages (list[PackageInfo]): list of PackageCompats to format.
145+ hide_parameters (list[str]): list of parameters to ignore in the output.
130146
131147 Returns:
132148 -------
@@ -148,27 +164,36 @@ def markdown(myLice: License, packages: list[PackageInfo]) -> str:
148164 strBuf .append (f"|{ '✔' if pkg .licenseCompat else '✖' } |{ pkg .name } |" )
149165
150166 # Details
167+ params_use_in_markdown = {
168+ "homePage" : "HomePage" ,
169+ "author" : "Author" ,
170+ "license" : "License" ,
171+ "licenseCompat" : "Compatible" ,
172+ "size" : "Size" ,
173+ }
151174 for pkg in packages :
175+ pkg_dict = pkg .get_filtered_dict (hide_parameters )
176+ pkg_dict_ordered_dict = OrderedDict ((param , pkg_dict [param ]) for param in params_use_in_markdown .keys () if param in pkg_dict )
152177 strBuf .extend (
153178 [
154- f"\n ### { pkg .namever } " ,
155- f"\n - HomePage: { pkg .homePage } " ,
156- f"- Author: { pkg .author } " ,
157- f"- License: { pkg .license } " ,
158- f"- Compatible: { pkg .licenseCompat } " ,
159- f"- Size: { pkg .size } " ,
179+ f"\n ### { pkg .namever } \n " ,
180+ * (
181+ f"- { params_use_in_markdown [k ]} : { v } "
182+ for k , v in pkg_dict_ordered_dict .items ()
183+ ),
160184 ]
161185 )
162186 return "\n " .join (strBuf ) + "\n "
163187
164188
165- def raw (myLice : License , packages : list [PackageInfo ]) -> str :
189+ def raw (myLice : License , packages : list [PackageInfo ], hide_parameters : list [ str ] = [] ) -> str :
166190 """Format to json.
167191
168192 Args:
169193 ----
170194 myLice (License): project license
171195 packages (list[PackageInfo]): list of PackageCompats to format.
196+ hide_parameters (list[str]): list of parameters to ignore in the output.
172197
173198 Returns:
174199 -------
@@ -179,19 +204,23 @@ def raw(myLice: License, packages: list[PackageInfo]) -> str:
179204 {
180205 "info" : INFO ,
181206 "project_license" : printLicense (myLice ),
182- "packages" : [x .__dict__ for x in packages ],
207+ "packages" : [
208+ x .get_filtered_dict (hide_parameters )
209+ for x in packages
210+ ],
183211 },
184212 indent = "\t " ,
185213 )
186214
187215
188- def rawCsv (myLice : License , packages : list [PackageInfo ]) -> str :
216+ def rawCsv (myLice : License , packages : list [PackageInfo ], hide_parameters : list [ str ] = [] ) -> str :
189217 """Format to csv.
190218
191219 Args:
192220 ----
193221 myLice (License): project license
194222 packages (list[PackageInfo]): list of PackageCompats to format.
223+ hide_parameters (list[str]): list of parameters to ignore in the output.
195224
196225 Returns:
197226 -------
@@ -202,7 +231,10 @@ def rawCsv(myLice: License, packages: list[PackageInfo]) -> str:
202231 string = StringIO ()
203232 writer = csv .DictWriter (string , fieldnames = list (packages [0 ].__dict__ ), lineterminator = "\n " )
204233 writer .writeheader ()
205- writer .writerows ([x .__dict__ for x in packages ])
234+ writer .writerows ([
235+ x .get_filtered_dict (hide_parameters )
236+ for x in packages
237+ ])
206238 return string .getvalue ()
207239
208240
0 commit comments