def init(init_args): '''Create a new blog application.''' module_path = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.dirname(module_path)) from DelogX.utils.config import Config cwd = os.getcwd() defaults = os.path.join(module_path, 'defaults') if os.listdir(cwd) and not init_args.force: print('''The current directory is not empty,\n''' '''use `delogx init --force' if you want to overwrite.''') return locale_dir = os.path.join(module_path, 'locale') locales = list() for lang in os.listdir(locale_dir): if os.path.isdir(os.path.join(locale_dir, lang)): locales.append(lang) locale = '' if init_args.locale and init_args.locale in locales: locale = init_args.locale elif init_args.locale: print('''Unknown locale '{}'. Abort.'''.format(init_args.locale)) sys.exit(1) else: while locale not in locales: print('Choose Language({0}):'.format(','.join(locales))) locale = input('> ') copytree(defaults, cwd) print('Setting language', locale) config = Config(os.path.join(cwd, 'config.json')) config.let('local.locale', locale) config.save() print('Creating demo post') post_dir = os.path.join(cwd, 'posts') print(' Creating {0}'.format(post_dir)) if os.path.isfile(post_dir): os.remove(post_dir) if not os.path.exists(post_dir): os.makedirs(post_dir) post_path = os.path.join(post_dir, 'hello-delogx.md') print(' Copying {0}'.format(post_path)) shutil.copyfile(os.path.join(locale_dir, locale, 'hello-delogx.md'), post_path) print('Creating demo page') page_dir = os.path.join(cwd, 'pages') print(' Creating {0}'.format(page_dir)) if os.path.isfile(page_dir): os.remove(page_dir) if not os.path.exists(page_dir): os.makedirs(page_dir) page_path = os.path.join(page_dir, 'demo.md') print(' Copying {0}'.format(page_path)) shutil.copyfile(os.path.join(locale_dir, locale, 'demo.md'), page_path) print('''Blog created successfully, edit 'config.json' to configure.''')
def __init__(self, app_path, framework, config='config.json'): '''Initialize DelogX object. Args: app_path (str): Absolute path of the blog application. framework (Flask): Flask application object. config (str): Config file of DelogX, defaults `config.json`. ''' self.framework = framework app_path = os.path.realpath(app_path) config = Path.abs_path(app_path, config) config = Config(config) runtime = Config() module_path = os.path.dirname(os.path.realpath(__file__)) runtime.let('path.app', app_path) runtime.let('path.module', module_path) ver_file = os.path.join(module_path, 'VERSION') with codecs.open(ver_file, encoding='utf-8') as f: version = ''.join(f.readlines()).strip() runtime.let('blog.version', version) self.config = config self.runtime = runtime self.markdown_ext = list() self.init_runtime() self.init_plugins() self.init_bundles() self.init_route() self.update_header()
class TestConfig(unittest.TestCase): def setUp(self): directory = os.path.dirname(os.path.realpath(__file__)) delogx = os.path.dirname(os.path.dirname(directory)) sys.path.insert(0, delogx) filename = os.path.join(directory, 'test_app', 'config.json') from DelogX.utils.config import Config self.config = Config(filename) def test_string(self): self.assertEqual(self.config.get('entry_a.key_1_1'), 'String') def test_number(self): self.assertEqual(self.config.get('entry_a.key_1_2'), 1000) self.assertAlmostEqual(self.config.get('entry_a.key_1_3'), -30.75) def test_null(self): self.assertIsNone(self.config.get('entry_b.key_2_1')) def test_bool(self): self.assertTrue(self.config.get('entry_b.key_2_2')) self.assertFalse(self.config.get('entry_b.key_2_3')) def test_list(self): equal_list = ["List_1_1", "List_1_2"] self.assertListEqual(self.config.get('entry_c.key_3_1'), equal_list) def test_nest(self): self.assertEqual(self.config.get('entry_c.key_3_2.key_3_2_1.test_1'), 'Test 1') self.assertEqual( self.config.get('entry_c.key_3_2.key_3_2_1.key_3_2_2.test_2'), 'Test 2') self.assertEqual(self.config.get('entry_c.key_3_2.key_3_2_3.test_3'), 'Test 3') def test_let(self): self.config.let('test', 'Test A') self.config.let('test2.a.b.c', 'Test B') self.config.let('entry_a.test', 'Test C') self.config.let('entry_a.test2', 'Test D') self.assertEqual(self.config.get('test'), 'Test A') self.assertEqual(self.config.get('test2.a.b.c'), 'Test B') self.assertEqual(self.config.get('entry_a.test'), 'Test C') self.assertEqual(self.config.get('entry_a.test2'), 'Test D') def test_delete(self): self.config.let('test', 'Test A') self.config.let('test2.a.b.c', 'Test B') self.assertEqual(self.config.get('test'), 'Test A') self.assertEqual(self.config.get('test2.a.b.c'), 'Test B') value_a = self.config.delete('test') value_b = self.config.delete('test2.a.b.c') self.assertEqual(value_a, 'Test A') self.assertEqual(value_b, 'Test B') self.assertIsNone(self.config.get('test')) self.assertIsNone(self.config.get('test2.a.b.c'))