Skip to content

Commit

Permalink
Finally an attempt at config file inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
9551-Dev committed Jun 28, 2024
1 parent 6eef69b commit 805fa85
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 217 deletions.
24 changes: 24 additions & 0 deletions assets/common_config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[Core]
template_path = assets/template.html
css_path = assets/style.css
js_path = assets/script.js

[Output]
images_directory_name = images
core_directory_name = core
output_file_name = index.html

[Index]
enabled = true
page = assets/index_template.html
base_directory = docs/g/
base_index_name = index.html

[Index.special_rules]
generate_project_index = true
generate_project_root_index = true

project_root_index_name = dir.html
project_index_name = index.html

root_index_name = index.html
25 changes: 1 addition & 24 deletions config/events/radioburza/brezen_2024.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,5 @@
title = Radioburza Březen 2024
image_folder = pictures/events/radioburza/brezen_2024

[Core]
template_path = assets/template.html
css_path = assets/style.css
js_path = assets/script.js

[Output]
output_folder = docs/g/events/radioburza/brezen_2024
images_directory_name = images
core_directory_name = core
output_file_name = index.html

[Index]
enabled = true
page = assets/index_template.html
base_directory = docs/g/
base_index_name = index.html

[Index.special_rules]
generate_project_index = true
generate_project_root_index = true

project_root_index_name = dir.html
project_index_name = index.html

root_index_name = index.html
output_folder = docs/g/events/radioburza/brezen_2024
25 changes: 1 addition & 24 deletions config/events/radioburza/kveten_2024.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,5 @@
title = Radioburza Květen 2024
image_folder = pictures/events/radioburza/kveten_2024

[Core]
template_path = assets/template.html
css_path = assets/style.css
js_path = assets/script.js

[Output]
output_folder = docs/g/events/radioburza/kveten_2024
images_directory_name = images
core_directory_name = core
output_file_name = index.html

[Index]
enabled = true
page = assets/index_template.html
base_directory = docs/g/
base_index_name = index.html

[Index.special_rules]
generate_project_index = true
generate_project_root_index = true

project_root_index_name = dir.html
project_index_name = index.html

root_index_name = index.html
output_folder = docs/g/events/radioburza/kveten_2024
25 changes: 1 addition & 24 deletions config/events/radioburza/leden_2024.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,5 @@
title = Radioburza Leden 2024
image_folder = pictures/events/radioburza/leden_2024

[Core]
template_path = assets/template.html
css_path = assets/style.css
js_path = assets/script.js

[Output]
output_folder = docs/g/events/radioburza/leden_2024
images_directory_name = images
core_directory_name = core
output_file_name = index.html

[Index]
enabled = true
page = assets/index_template.html
base_directory = docs/g/
base_index_name = index.html

[Index.special_rules]
generate_project_index = true
generate_project_root_index = true

project_root_index_name = dir.html
project_index_name = index.html

root_index_name = index.html
output_folder = docs/g/events/radioburza/leden_2024
25 changes: 1 addition & 24 deletions config/lab/storage_2024_03_07.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,5 @@
title = The state of my box storage taken at 2024.03.07
image_folder = pictures/lab/storage_2024_03_07

[Core]
template_path = assets/template.html
css_path = assets/style.css
js_path = assets/script.js

[Output]
output_folder = docs/g/lab/storage_2024_03_07
images_directory_name = images
core_directory_name = core
output_file_name = index.html

[Index]
enabled = true
page = assets/index_template.html
base_directory = docs/g/
base_index_name = index.html

[Index.special_rules]
generate_project_index = true
generate_project_root_index = true

project_root_index_name = dir.html
project_index_name = index.html

root_index_name = index.html
output_folder = docs/g/lab/storage_2024_03_07
25 changes: 1 addition & 24 deletions config/misc/pat_a_mat.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,8 @@ title = Pat a mat memes
page_title = Pat a Mat
image_folder = pictures/misc/pat_a_mat

[Core]
template_path = assets/template.html
css_path = assets/style.css
js_path = assets/script.js

[Output]
output_folder = docs/g/misc/pat_a_mat
images_directory_name = images
core_directory_name = core
output_file_name = index.html

[Index]
enabled = true
page = assets/index_template.html
base_directory = docs/g/
base_index_name = index.html

[Index.special_rules]
generate_project_index = true
generate_project_root_index = true

project_root_index_name = dir.html
project_index_name = index.html

root_index_name = index.html
output_folder = docs/g/misc/pat_a_mat

[Embed]
enabled = true
Expand Down
25 changes: 1 addition & 24 deletions config/projects/hfvttc_v1.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,8 @@ title = HFVTTC <span style='color:DodgerBlue'>V1</span>
page_title = HFVTTC V1
image_folder = pictures/projects/hfvttc_v1

[Core]
template_path = assets/template.html
css_path = assets/style.css
js_path = assets/script.js

[Output]
output_folder = docs/g/projects/hfvttc_v1
images_directory_name = images
core_directory_name = core
output_file_name = index.html

[Index]
enabled = true
page = assets/index_template.html
base_directory = docs/g/
base_index_name = index.html

[Index.special_rules]
generate_project_index = true
generate_project_root_index = true

project_root_index_name = dir.html
project_index_name = index.html

root_index_name = index.html
output_folder = docs/g/projects/hfvttc_v1

[Embed]
enabled = true
Expand Down
25 changes: 1 addition & 24 deletions config/projects/sstc_v1.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,8 @@ title = SSTC V1
page_title = SSTC V1
image_folder = pictures/projects/sstc_v1

[Core]
template_path = assets/template.html
css_path = assets/style.css
js_path = assets/script.js

[Output]
output_folder = docs/g/projects/sstc_v1
images_directory_name = images
core_directory_name = core
output_file_name = index.html

[Index]
enabled = true
page = assets/index_template.html
base_directory = docs/g/
base_index_name = index.html

[Index.special_rules]
generate_project_index = true
generate_project_root_index = true

project_root_index_name = dir.html
project_index_name = index.html

root_index_name = index.html
output_folder = docs/g/projects/sstc_v1

[Embed]
enabled = true
Expand Down
97 changes: 58 additions & 39 deletions gallery_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,70 +168,83 @@ def remove_base_dir(index, path):
else:
return path

def config_get_default(config,group,value,*default):
def config_get_default(config,parent_config,group,value,*default):
if config.has_option(group,value):
return config.get(group,value)
else:
elif hasattr(parent_config,"has_option") and parent_config.has_option(group,value):
return parent_config.get(group,value)
elif default:
return default[0]
else:
raise ValueError(f"Default value not provided for {group}.{value}")


def config_get_section(config, section):
if config.has_section(section):
return dict(config.items(section))
else:
return {}

def read_config(config_path):
def read_config(config_path,*parent_config_path):
print("Reading configuration...")
config = configparser.ConfigParser()
config = configparser.ConfigParser()
parent_config = configparser.ConfigParser()

config.read(config_path)
if parent_config_path[0]:
if os.path.exists(parent_config_path[0]):
parent_config.read(parent_config_path[0])
print(f"Parent config file {parent_config_path[0]} found and loaded.")
else:
print(f"Parent config file {parent_config_path[0]} not found.")

settings = {
"title": config_get_default(config,"Settings","title","Gallery title!"),
"image_folder": config_get_default(config,"Settings","image_folder","pictures"),
"page_title": config_get_default(config,"Settings","page_title",config_get_default(config,"Settings","title","Gallery title!")),
"title": config_get_default(config,parent_config,"Settings","title","Gallery title!"),
"image_folder": config_get_default(config,parent_config,"Settings","image_folder","pictures"),
"page_title": config_get_default(config,parent_config,"Settings","page_title",config_get_default(config,parent_config,"Settings","title","Gallery title!")),
}

embed = {
"enabled" : config_get_default(config,"Embed","enabled","false") == "true",
"title" : config_get_default(config,"Embed","title","An image gallery"),
"description": config_get_default(config,"Embed","description","A very filled image gallery with images"),
"image" : config_get_default(config,"Embed","image","https://github.com/9551-Dev.png"),
"color" : config_get_default(config,"Embed","color","#90f91f"),
"url" : config_get_default(config,"Embed","url","https://github.com/9551-Dev")
"enabled" : config_get_default(config,parent_config,"Embed","enabled","false") == "true",
"title" : config_get_default(config,parent_config,"Embed","title","An image gallery"),
"description": config_get_default(config,parent_config,"Embed","description","A very filled image gallery with images"),
"image" : config_get_default(config,parent_config,"Embed","image","https://github.com/9551-Dev.png"),
"color" : config_get_default(config,parent_config,"Embed","color","#90f91f"),
"url" : config_get_default(config,parent_config,"Embed","url","https://github.com/9551-Dev")
}

index = {
"index_style": config_get_default(config,"Index","page","assets/index_template.html"),
"enabled": config_get_default(config,"Index","enabled","true") == "true",
"base_directory": config_get_default(config,"Index","base_directory","docs"),
"base_index_name": config_get_default(config,"Index","base_index_name","index.html"),

"generate_project_index": config_get_default(config,"Index.special_rules","generate_project_index","false"),
"generate_project_root_index": config_get_default(config,"Index.special_rules","generate_project_root_index","false"),
"project_index_name": config_get_default(config,"Index.special_rules","project_index_name","index.html"),
"project_root_index_name": config_get_default(config,"Index.special_rules","project_root_index_name","dir.html"),

"root_index_name": config_get_default(config,"Index.special_rules","root_index_name","root_index.html"),
"root_index_super": config_get_default(config,"Index.special_rules","root_index_super","UNUSED"),
"index_style": config_get_default(config,parent_config,"Index","page","assets/index_template.html"),
"enabled": config_get_default(config,parent_config,"Index","enabled","true") == "true",
"base_directory": config_get_default(config,parent_config,"Index","base_directory","docs"),
"base_index_name": config_get_default(config,parent_config,"Index","base_index_name","index.html"),

"generate_project_index": config_get_default(config,parent_config,"Index.special_rules","generate_project_index","false"),
"generate_project_root_index": config_get_default(config,parent_config,"Index.special_rules","generate_project_root_index","false"),
"project_index_name": config_get_default(config,parent_config,"Index.special_rules","project_index_name","index.html"),
"project_root_index_name": config_get_default(config,parent_config,"Index.special_rules","project_root_index_name","dir.html"),

"root_index_name": config_get_default(config,parent_config,"Index.special_rules","root_index_name","root_index.html"),
"root_index_super": config_get_default(config,parent_config,"Index.special_rules","root_index_super","UNUSED"),
}

core = {
"template_path": config_get_default(config,"Core","template_path"),
"css_path": config_get_default(config,"Core","css_path"),
"js_path": config_get_default(config,"Core","js_path")
"template_path": config_get_default(config,parent_config,"Core","template_path"),
"css_path": config_get_default(config,parent_config,"Core","css_path"),
"js_path": config_get_default(config,parent_config,"Core","js_path")
}

output = {
"output_folder": config_get_default(config,"Output","output_folder","PLS_CONFIGURE_THX_:3"),
"images_directory_name": config_get_default(config,"Output","images_directory_name","images"),
"core_directory_name": config_get_default(config,"Output","core_directory_name","core"),
"output_file_name": config_get_default(config,"Output","output_file_name","index.html")
"output_folder": config_get_default(config,parent_config,"Output","output_folder","PLS_CONFIGURE_THX_:3"),
"images_directory_name": config_get_default(config,parent_config,"Output","images_directory_name","images"),
"core_directory_name": config_get_default(config,parent_config,"Output","core_directory_name","core"),
"output_file_name": config_get_default(config,parent_config,"Output","output_file_name","index.html")
}

pagefile = {
"enabled": config_get_default(config,"Pagefile","enabled","false") == "true",
"source_file": config_get_default(config,"Pagefile","source","NONE"),
"image_descriptors": config_get_section(config, "Pagefile.descriptions")
"enabled": config_get_default(config,parent_config,"Pagefile","enabled","false") == "true",
"source_file": config_get_default(config,parent_config,"Pagefile","source","NONE"),
"image_descriptors": config_get_section(config,"Pagefile.descriptions")
}

required_files = [settings['image_folder'], core['template_path'], core['css_path'], core['js_path']]
Expand Down Expand Up @@ -509,12 +522,18 @@ def generate_directory_indexes(output_folder,output,index):
generate_directory_index(index["base_directory"] or output["output_folder"].split(os.sep)[0],index,index["root_index_name"] or "index.html")

if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python script.py [config_path]")
if len(sys.argv) < 2:
print("Usage: python script.py <config_path/parent_config_path> [config_path_sub]")
exit(1)

config_path = sys.argv[1]
settings,core,output,index,embed,pagefile = read_config(config_path)
parent_config_path = None
config_path = sys.argv[1]

if len(sys.argv) >= 3:
parent_config_path = sys.argv[1]
config_path = sys.argv[2]

settings,core,output,index,embed,pagefile = read_config(config_path,parent_config_path)

print(f'\nTitle: {settings["page_title"]}')
print(f'Index files: {index["enabled"] and "enabled" or "disabled"} ({index["enabled"]})')
Expand Down
Loading

0 comments on commit 805fa85

Please sign in to comment.