Add json
This commit is contained in:
		@@ -49,7 +49,7 @@ options:
 | 
				
			|||||||
                        Filename where to export the blocklist
 | 
					                        Filename where to export the blocklist
 | 
				
			||||||
  -v, --verbose
 | 
					  -v, --verbose
 | 
				
			||||||
  -n, --no-delete       Do not delete existing blocks
 | 
					  -n, --no-delete       Do not delete existing blocks
 | 
				
			||||||
  --format FORMAT       Export format: toml|markdown|csv
 | 
					  --format FORMAT       Export format: toml|markdown|csv|json
 | 
				
			||||||
  --private             When the flag is set, private comment will also be exported.
 | 
					  --private             When the flag is set, private comment will also be exported.
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,7 @@ import os
 | 
				
			|||||||
import toml
 | 
					import toml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from mastodon_blocklist_deploy.models import Instance
 | 
					from mastodon_blocklist_deploy.models import Instance
 | 
				
			||||||
from mastodon_blocklist_deploy.helpers import blocklist_to_markdown, blocklist_to_toml, blocklist_to_csv
 | 
					from mastodon_blocklist_deploy.helpers import blocklist_to_markdown, blocklist_to_toml, blocklist_to_csv, blocklist_to_json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def load_blocklist_file(filename: str) -> [Instance]:
 | 
					def load_blocklist_file(filename: str) -> [Instance]:
 | 
				
			||||||
@@ -52,6 +52,8 @@ def exporter(blocklist, output=None, format: str = "toml", private: bool = False
 | 
				
			|||||||
        exported_text = blocklist_to_csv(blocklist, private)
 | 
					        exported_text = blocklist_to_csv(blocklist, private)
 | 
				
			||||||
    if format == "markdown":
 | 
					    if format == "markdown":
 | 
				
			||||||
        exported_text = blocklist_to_markdown(blocklist, private)
 | 
					        exported_text = blocklist_to_markdown(blocklist, private)
 | 
				
			||||||
 | 
					    if format == "json":
 | 
				
			||||||
 | 
					        exported_text = blocklist_to_json(blocklist, private)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Output the text
 | 
					    # Output the text
 | 
				
			||||||
    if output is not None:
 | 
					    if output is not None:
 | 
				
			||||||
@@ -76,7 +78,7 @@ def cli():
 | 
				
			|||||||
    parser.add_argument('-o', '--output', help="Filename where to export the blocklist")
 | 
					    parser.add_argument('-o', '--output', help="Filename where to export the blocklist")
 | 
				
			||||||
    parser.add_argument('-v', '--verbose', action='store_true')
 | 
					    parser.add_argument('-v', '--verbose', action='store_true')
 | 
				
			||||||
    parser.add_argument('-n', '--no-delete', action='store_true', help="Do not delete existing blocks")
 | 
					    parser.add_argument('-n', '--no-delete', action='store_true', help="Do not delete existing blocks")
 | 
				
			||||||
    parser.add_argument('--format', help="Export format: toml|markdown|csv")
 | 
					    parser.add_argument('--format', help="Export format: toml|markdown|csv|json")
 | 
				
			||||||
    parser.add_argument('--private', action='store_true', help="When the flag is set, private comment will also be "
 | 
					    parser.add_argument('--private', action='store_true', help="When the flag is set, private comment will also be "
 | 
				
			||||||
                                                               "exported.")
 | 
					                                                               "exported.")
 | 
				
			||||||
    args = parser.parse_args()
 | 
					    args = parser.parse_args()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ from mastodon_blocklist_deploy.models import Instance
 | 
				
			|||||||
import toml
 | 
					import toml
 | 
				
			||||||
import io
 | 
					import io
 | 
				
			||||||
import csv
 | 
					import csv
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def blocklist_to_markdown(blocklist: [Instance], private: bool = False):
 | 
					def blocklist_to_markdown(blocklist: [Instance], private: bool = False):
 | 
				
			||||||
    if private:
 | 
					    if private:
 | 
				
			||||||
@@ -29,3 +30,7 @@ def blocklist_to_csv(blocklist: [Instance], private: bool = False):
 | 
				
			|||||||
    w.writeheader()
 | 
					    w.writeheader()
 | 
				
			||||||
    w.writerows(blocklist_as_dict)
 | 
					    w.writerows(blocklist_as_dict)
 | 
				
			||||||
    return csv_string.getvalue()
 | 
					    return csv_string.getvalue()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def blocklist_to_json(blocklist: [Instance], private: bool = False):
 | 
				
			||||||
 | 
					    json_string = json.dumps([b.as_dict(private) for b in blocklist])
 | 
				
			||||||
 | 
					    return json_string
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user