annotate piecrust/pathutil.py @ 1063:21834ab8fd9b

cm: Bump Inukshuk to `0.1.2`, regenerate `requirements.txt`.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 13 Feb 2018 21:25:46 -0800
parents 3ceeca7bb71c
children 8af2ea1f5c34
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 if not root:
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 if cwd is None:
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 cwd = os.getcwd()
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 return cwd
a212a3f2e3ee Initial commit.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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