Skip to content

Commit

Permalink
refactor: new config style
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanDecode committed Dec 17, 2019
1 parent 7f33143 commit 338ab8a
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 143 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ cached_imgs
tmp
temp
test_dist
config.py

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ install:
- make clean
- make dep
script:
- cp ./config.sample.py ./config.py
- make all
after_script:
## Push to preview site
Expand Down
1 change: 0 additions & 1 deletion Maverick/Builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import math
import json

import config
from .Utils import logged_func, print_color, Color, safe_write, \
safe_read, unify_joinpath, copytree, gen_hash
from .Metadata import Metadata
Expand Down
4 changes: 2 additions & 2 deletions Maverick/Cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from PIL import Image
from PIL import ImageFile
import urllib
import config
from .Config import g_conf

from urllib.parse import urlparse
import urllib.request as request
Expand Down Expand Up @@ -101,7 +101,7 @@ def log_and_return(filename):

# if it is remote image
if src.startswith('http'):
if config.Config.fetch_remote_imgs:
if g_conf.fetch_remote_imgs:
# download and treat it as local image
try:
suffix = urlparse(src).path.split('.')[-1]
Expand Down
75 changes: 75 additions & 0 deletions Maverick/Config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
"""Configuration
"""

import os
import sys


class Config(object):
def update_fromfile(self, filepath=None):
if filepath is None:
return

sys.path.append(os.path.dirname(filepath))

name, _ = os.path.splitext(os.path.basename(filepath))
module = __import__(name)

attrs = dir(module)
for attr in attrs:
if attr.startswith('__'):
continue
setattr(self, attr, getattr(module, attr))

def update_fromenv(self):
attrs = dir(self)
for attr in attrs:
if attr.startswith('__'):
continue
setattr(self, attr, os.getenv(attr, getattr(self, attr)))

# For Maverick
site_prefix = "/"
source_dir = "./test_src/"
build_dir = "./test_dist/"
template = "Galileo"
index_page_size = 10
archives_page_size = 30
fetch_remote_imgs = False
locale = "Asia/Shanghai"

# For site
site_name = ""
site_logo = ""
site_build_date = ""
author = ""
email = ""
author_homepage = ""
description = ""
key_words = []
language = "english"
background_img = ""
external_links = []
nav = []

social_links = []

valine = {
"enable": False,
"appId": "",
"appKey": "",
"notify": "false",
"visitor": "false",
"recordIP": "false",
"serverURLs": None,
"placeholder": "Just go go~"
}

head_addon = ""

footer_addon = ""

body_addon = ""

g_conf = Config()
2 changes: 1 addition & 1 deletion Maverick/Metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
import moment
from config import g_conf
from .Config import g_conf

class Metadata(dict):
"""Metadata
Expand Down
4 changes: 2 additions & 2 deletions Maverick/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import hashlib
import chardet
import json
import config
from .Config import g_conf
from enum import Enum


Expand Down Expand Up @@ -97,7 +97,7 @@ def tr(str, locale="english"):
"""
global g_translation
if g_translation is None:
path = unify_joinpath('./locale', config.g_conf.language+".json")
path = unify_joinpath('./locale', g_conf.language+".json")
g_translation = json.loads(safe_read(path) or '{}')

return g_translation.get(str, str)
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ Install dependencies:
pip install -r prod_req.txt
```

If error occurs, please verify your Python and pip version. Then get a copy of sample configuration file:
If error occurs, please verify your Python and pip version. Then edit the default configuration file:

```bash
cp ./config.sample.py ./config.py
vi ./config.py
```

For now let's use the default settings. Type this command in your terminal:
Expand Down Expand Up @@ -94,6 +94,14 @@ Although Maverick is much simpler than many other generators, it does have a few

Note: You can access other options by `${option_name}`. For example `${site_prefix}logo.png` will be parsed as `/logo.png`.

Note: you can also use configuration file other than `config.py`, just specify it when build:

```bash
python ./build.py -c "./my_conf.py"
# or
python ./build.py --config "./my_conf.py"
```

### Options for Maverick

| Option | Default Value | Explanation |
Expand Down
4 changes: 2 additions & 2 deletions Templates/Galileo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import re
import os
import config
from Maverick.Config import g_conf
from Maverick.Utils import tr


Expand Down Expand Up @@ -58,7 +58,7 @@ def getKey(str):
if value is None:
# find in config
try:
value = getattr(config.g_conf, key)
value = getattr(g_conf, key)
except AttributeError:
pass

Expand Down
35 changes: 23 additions & 12 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,38 @@
GitHub: https://github.com/AlanDecode
"""

from Maverick.Utils import print_color, Color
from Maverick.Builder import Builder
from Maverick.Config import g_conf
import os
import sys
import getopt

if not os.path.exists('./config.py'):
raise BaseException('No config.py! Exiting...')

def main(argv):
try:
opts, _ = getopt.getopt(argv, "c:", "config=")
except getopt.GetoptError:
pass

import config
from Maverick.Builder import Builder
from Maverick.Utils import print_color, Color
opts = dict(opts)

if '-c' in opts or '--config' in opts:
g_conf.update_fromfile(opts.get('-c', None))
g_conf.update_fromfile(opts.get('--config', None))
else:
if os.path.exists('./config.py'):
g_conf.update_fromfile('./config.py')

def main():
conf = config.g_conf
g_conf.update_fromenv()

print_color('Site prefix: ' + conf.site_prefix, Color.GREEN.value)
print_color('Source dir: ' + conf.source_dir, Color.GREEN.value)
print_color('Build dir: ' + conf.build_dir, Color.GREEN.value)
print_color('Site prefix: ' + g_conf.site_prefix, Color.GREEN.value)
print_color('Source dir: ' + g_conf.source_dir, Color.GREEN.value)
print_color('Build dir: ' + g_conf.build_dir, Color.GREEN.value)

builder = Builder(conf)
builder = Builder(g_conf)
builder.build_all()


if __name__ == "__main__":
main()
main(sys.argv[1:])
95 changes: 95 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# -*- coding: utf-8 -*-
"""Sample Configuration
"""

# For Maverick
site_prefix = "/"
source_dir = "./test_src/"
build_dir = "./test_dist/"
template = "Galileo"
index_page_size = 10
archives_page_size = 30
fetch_remote_imgs = False
locale = "Asia/Shanghai"

# For site
site_name = "Maverick"
site_logo = "${site_prefix}android-chrome-512x512.png"
site_build_date = "2019-12-06T12:00+08:00"
author = "AlanDecode"
email = "[email protected]"
author_homepage = "https://www.imalan.cn"
description = "This is Maverick, Theme Galileo."
key_words = ["Maverick", "AlanDecode", "Galileo", "blog"]
language = 'english'
background_img = '${site_prefix}bg/The_Great_Wave_off_Kanagawa.jpg'
external_links = [
{
"name": "AlanDecode/Maverick",
"url": "https://github.com/AlanDecode/Maverick",
"brief": "🏄‍ Go My Own Way."
},
{
"name": "Triple NULL",
"url": "https://www.imalan.cn",
"brief": "Home page for AlanDecode."
}
]
nav = [
{
"name": "Home",
"url": "${site_prefix}",
"target": "_self"
},
{
"name": "Archives",
"url": "${site_prefix}archives/",
"target": "_self"
},
{
"name": "About",
"url": "${site_prefix}about/",
"target": "_self"
}
]

social_links = [
{
"name": "Twitter",
"url": "https://twitter.com/AlanDecode",
"icon": "gi gi-twitter"
},
{
"name": "GitHub",
"url": "https://github.com/AlanDecode",
"icon": "gi gi-github"
},
{
"name": "Weibo",
"url": "https://weibo.com/5245109677/",
"icon": "gi gi-weibo"
}
]

valine = {
"enable": False,
"appId": "",
"appKey": "",
"notify": "false",
"visitor": "false",
"recordIP": "false",
"serverURLs": None,
"placeholder": "Just go go~"
}

head_addon = r"""
<!-- Content here will be added before </head>. -->
"""

footer_addon = r"""
<!-- Content here will be added to <footer> tag.-->
"""

body_addon = r"""
<!-- Content here will be added before </body>. -->
"""
Loading

0 comments on commit 338ab8a

Please sign in to comment.