|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "8bbf8f85-3a07-4b4b-82fe-75f51e54caa1", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "This is free and unencumbered software released into the public domain.\n", |
| 9 | + "\n", |
| 10 | + "Anyone is free to copy, modify, publish, use, compile, sell, or\n", |
| 11 | + "distribute this software, either in source code form or as a compiled\n", |
| 12 | + "binary, for any purpose, commercial or non-commercial, and by any\n", |
| 13 | + "means.\n", |
| 14 | + "\n", |
| 15 | + "In jurisdictions that recognize copyright laws, the author or authors\n", |
| 16 | + "of this software dedicate any and all copyright interest in the\n", |
| 17 | + "software to the public domain. We make this dedication for the benefit\n", |
| 18 | + "of the public at large and to the detriment of our heirs and\n", |
| 19 | + "successors. We intend this dedication to be an overt act of\n", |
| 20 | + "relinquishment in perpetuity of all present and future rights to this\n", |
| 21 | + "software under copyright law.\n", |
| 22 | + "\n", |
| 23 | + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n", |
| 24 | + "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n", |
| 25 | + "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n", |
| 26 | + "IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR\n", |
| 27 | + "OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\n", |
| 28 | + "ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\n", |
| 29 | + "OTHER DEALINGS IN THE SOFTWARE.\n", |
| 30 | + "\n", |
| 31 | + "For more information, please refer to <https://unlicense.org>" |
| 32 | + ] |
| 33 | + }, |
3 | 34 | { |
4 | 35 | "cell_type": "code", |
5 | | - "execution_count": 1, |
| 36 | + "execution_count": null, |
6 | 37 | "id": "8737b6b4-a572-46b2-bd22-b70cfccb501d", |
7 | 38 | "metadata": {}, |
8 | 39 | "outputs": [], |
9 | 40 | "source": [ |
10 | | - "from pathlib import Path\n", |
11 | | - "import re\n", |
12 | | - "import json\n", |
13 | | - "import sys" |
| 41 | + "from pathlib import Path # Importing Path class from pathlib for file path manipulations\n", |
| 42 | + "import re # Importing re for regular expression operations\n", |
| 43 | + "import json # Importing json for handling JSON data\n", |
| 44 | + "import sys # Importing sys for command line argument handling" |
14 | 45 | ] |
15 | 46 | }, |
16 | 47 | { |
|
19 | 50 | "id": "8373a7f1-49fe-4279-8a35-fadb413acde0", |
20 | 51 | "metadata": {}, |
21 | 52 | "outputs": [], |
22 | | - "source": [] |
| 53 | + "source": [ |
| 54 | + "def show_help():\n", |
| 55 | + " \"\"\"Display the help message for the script.\"\"\"\n", |
| 56 | + " print(\"Usage:\")\n", |
| 57 | + " print(\"./ParsingMetadataMD2JSON [options] <input file>...\")\n", |
| 58 | + " print(\"Options:\")\n", |
| 59 | + " print(\"--help Show this screen.\")\n", |
| 60 | + " print()" |
| 61 | + ] |
23 | 62 | }, |
24 | 63 | { |
25 | 64 | "cell_type": "code", |
26 | | - "execution_count": 2, |
| 65 | + "execution_count": null, |
27 | 66 | "id": "ee24ff0d-07b7-4b21-8f38-211783deb980", |
28 | 67 | "metadata": {}, |
29 | 68 | "outputs": [], |
30 | 69 | "source": [ |
31 | | - "\"\"\"\n", |
32 | | - "Usage:\n", |
33 | | - " ./ParsingMetadataMD2JSON [options] <input file>...\n", |
34 | | - "Options:\n", |
35 | | - " --help Show this screen.\n", |
36 | | - "\"\"\"\n", |
37 | | - "\n", |
38 | 70 | "def main() -> int:\n", |
39 | | - " # Reads in a MarkDown M_datafile.md parsing every section\n", |
40 | | - " # into a separate JSON M_datafile.json file\n", |
| 71 | + " \"\"\"\n", |
| 72 | + " Reads in a MarkDown M_datafile.md parsing every section\n", |
| 73 | + " into a separate JSON M_datafile.json file\n", |
| 74 | + " \"\"\"\n", |
41 | 75 | "\n", |
42 | 76 | " # Get command line arguments for file\n", |
43 | 77 | " args = sys.argv[1:]\n", |
44 | 78 | " \n", |
| 79 | + " # Check for help option\n", |
| 80 | + " if '--help' in args:\n", |
| 81 | + " show_help() # Call the help function\n", |
| 82 | + " return 0 # Exit after showing help\n", |
| 83 | + " \n", |
45 | 84 | " # Get the file to convert\n", |
46 | 85 | " try:\n", |
47 | 86 | " md_path = Path(args[0])\n", |
48 | 87 | " if not md_path.exists():\n", |
49 | 88 | " raise FileNotFoundError(f\"{md_path} does not exist.\")\n", |
50 | 89 | " except IndexError:\n", |
51 | | - " print(\"Usage:\")\n", |
52 | | - " print(\"./ParsingMetadataMD2JSON [options] <input file>...\")\n", |
53 | | - " print(\"Options:\")\n", |
54 | | - " print(\"--help Show this screen.\")\n", |
55 | | - " print()\n", |
56 | | - " #raise IndexError(f\"Expected file name as first argument\")\n", |
| 90 | + " # Handle the case where no file is provided\n", |
57 | 91 | " print(f\"Expected file name as first argument...EXITING...\")\n", |
58 | | - " sys.exit(1) # something went wrong\n", |
| 92 | + " show_help() # Show help message\n", |
| 93 | + " sys.exit(1) # Exit the program with an error code as something went wrong\n", |
59 | 94 | " \n", |
60 | 95 | " with open(str(md_path), \"r\", encoding=\"utf-8\") as f:\n", |
61 | 96 | " contents = f.read()#.replace(\"\\r\", \"\") # Remove Windows style line breaks\n", |
|
68 | 103 | " with open(Path(args[0][:-len(\".md\")] + \".json\"), 'w', encoding='utf-8') as f:\n", |
69 | 104 | " json.dump(dictionaryJSON, f, ensure_ascii=False, indent=4)\n", |
70 | 105 | " \n", |
| 106 | + " # Print success message indicating the output file\n", |
71 | 107 | " print(\"SUCCESS: \" + str(Path(args[0]))+\" parsed to \" + str(Path(args[0][:-len(\".md\")] + \".json\")) )\n", |
72 | 108 | "\n", |
73 | 109 | " return 0 # success\n" |
74 | 110 | ] |
75 | 111 | }, |
76 | 112 | { |
77 | 113 | "cell_type": "code", |
78 | | - "execution_count": 3, |
| 114 | + "execution_count": null, |
79 | 115 | "id": "650d3f63-758a-49b6-9663-338dcc47f653", |
80 | 116 | "metadata": {}, |
81 | 117 | "outputs": [], |
|
96 | 132 | }, |
97 | 133 | { |
98 | 134 | "cell_type": "code", |
99 | | - "execution_count": 4, |
| 135 | + "execution_count": null, |
100 | 136 | "id": "f9f69886-9c77-4017-90bb-6f9e9ee82d42", |
101 | 137 | "metadata": {}, |
102 | 138 | "outputs": [], |
|
106 | 142 | " # change, amend, or replace to your needs\n", |
107 | 143 | "\n", |
108 | 144 | " # Initialize variables to store the JSON data\n", |
109 | | - " M_title = {}\n", |
110 | | - " M_creators = [] # can be a list\n", |
111 | | - " M_publisher = {}\n", |
112 | | - " M_contributors = [] # can be a list\n", |
113 | | - " M_description = {}\n", |
114 | | - " M_subjects = [] # a list of keywords\n", |
115 | | - " M_date = {}\n", |
116 | | - " M_language = [] # a list of languages\n", |
117 | | - " M_formats = [] # a list of file formats\n", |
118 | | - " M_type = {}\n", |
119 | | - " M_coverage = {}\n", |
120 | | - " M_identifier = {}\n", |
121 | | - " M_methods = []\n", |
122 | | - " M_sources = []\n", |
123 | | - " M_relations = []\n", |
124 | | - " M_rights = []\n", |
| 145 | + " M_title = {} # the title of entry\n", |
| 146 | + " M_creators = [] # can be a list of creators\n", |
| 147 | + " M_publisher = {} # the publisher\n", |
| 148 | + " M_contributors = [] # can be a list of contributors\n", |
| 149 | + " M_description = {} # the description of entry\n", |
| 150 | + " M_subjects = [] # a list of keywords\n", |
| 151 | + " M_date = {} # the last-changed date\n", |
| 152 | + " M_language = [] # a list of languages\n", |
| 153 | + " M_formats = [] # a list of file formats\n", |
| 154 | + " M_type = {} # the type of entry\n", |
| 155 | + " M_coverage = {} # the period of time\n", |
| 156 | + " M_identifier = {} # unique identifier (PID)\n", |
| 157 | + " M_methods = [] # a list of methods for creating the entry\n", |
| 158 | + " M_sources = [] # a list of sources of the entry\n", |
| 159 | + " M_relations = [] # a list of relations about the entry\n", |
| 160 | + " M_rights = [] # a refernce of licence statements\n", |
125 | 161 | " \n", |
126 | 162 | " \n", |
127 | 163 | " for index, entry in enumerate(contentdictionary):\n", |
|
270 | 306 | }, |
271 | 307 | { |
272 | 308 | "cell_type": "code", |
273 | | - "execution_count": 5, |
| 309 | + "execution_count": null, |
274 | 310 | "id": "32674c9d-4060-4fcb-bfe0-97b922d64227", |
275 | 311 | "metadata": {}, |
276 | | - "outputs": [ |
277 | | - { |
278 | | - "name": "stdout", |
279 | | - "output_type": "stream", |
280 | | - "text": [ |
281 | | - "SUCCESS: M_Dataset_README_Example.md parsed to M_Dataset_README_Example.json\n" |
282 | | - ] |
283 | | - } |
284 | | - ], |
| 312 | + "outputs": [], |
285 | 313 | "source": [ |
286 | 314 | "%%python3 ParsingMetadataMD2JSON.py ./M_Dataset_README_Example.md\n", |
287 | 315 | "# cell magic to fake command line execution\n", |
|
0 commit comments