Mercurial > piecrust2
annotate piecrust/pathutil.py @ 1108:b2a34a6ec5e5
data: Let the `asset` endpoint load JSON data into the template engine.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 18 Feb 2018 20:32:30 -0800 |
parents | 3ceeca7bb71c |
children | 8af2ea1f5c34 |
rev | line source |
---|---|
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
1 import re |
0 | 2 import os |
3 import os.path | |
62
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
4 import fnmatch |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
5 from piecrust import CONFIG_PATH, THEME_CONFIG_PATH |
0 | 6 |
7 | |
115
9c074aec60a6
Fix search for root folder. Must have been drunk when I wrote this originally.
Ludovic Chabant <ludovic@chabant.com>
parents:
62
diff
changeset
|
8 re_terminal_path = re.compile(r'^(\w\:)?[/\\]$') |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
9 |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
10 |
0 | 11 class SiteNotFoundError(Exception): |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
12 def __init__(self, root=None, msg=None, theme=False): |
0 | 13 if not root: |
14 root = os.getcwd() | |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
15 |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
16 cfg_name = CONFIG_PATH |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
17 if theme: |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
18 cfg_name = THEME_CONFIG_PATH |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
19 |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
20 full_msg = ("No PieCrust website in '%s' " |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
21 "('%s' not found!)" % (root, cfg_name)) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
22 if msg: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
23 full_msg += ": " + msg |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
24 else: |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
25 full_msg += "." |
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
26 Exception.__init__(self, full_msg) |
0 | 27 |
28 | |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
29 def find_app_root(cwd=None, theme=False): |
0 | 30 if cwd is None: |
31 cwd = os.getcwd() | |
32 | |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
33 cfg_name = CONFIG_PATH |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
34 if theme: |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
35 cfg_name = THEME_CONFIG_PATH |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
36 |
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
37 while not os.path.isfile(os.path.join(cwd, cfg_name)): |
0 | 38 cwd = os.path.dirname(cwd) |
38
091f99bfbe44
Fix running `chef` outside of a website. Slightly better error reporting.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
39 if not cwd or re_terminal_path.match(cwd): |
663
3ceeca7bb71c
themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents:
115
diff
changeset
|
40 raise SiteNotFoundError(cwd, theme=theme) |
0 | 41 return cwd |
42 | |
62
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
43 |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
44 def multi_fnmatch_filter(names, patterns, modifier=None, inverse=True): |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
45 res = [] |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
46 for n in names: |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
47 matches = False |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
48 test_n = modifier(n) if modifier else n |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
49 for p in patterns: |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
50 if fnmatch.fnmatch(test_n, p): |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
51 matches = True |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
52 break |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
53 if matches and not inverse: |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
54 res.append(n) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
55 elif not matches and inverse: |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
56 res.append(n) |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
57 return res |
52e4d9a1f917
Simple importer for PieCrust 1 websites.
Ludovic Chabant <ludovic@chabant.com>
parents:
38
diff
changeset
|
58 |