Welcome to fptokens documentation!¶
Contents:
Getting Started¶
To get started with fptokens
, type:
import fptokens as fpt
To create a file name, type:
filename = fpt.Filename(root='/Users/demo/Desktop',
folders=['assets', '$colors$'],
base=['asset', '$colors$', '1200px'])
This created a file name with default settings, _
as the separator,
jpg
as the extension and $
as the escape character for the tokens.
To parse and convert the tokens of the file name to actual tokens, type:
filename.parse()
To get the results of the parsing, type:
print filename.tokens
[<Token: $color$>]
The list of tokens could now be used to create permutations of the tokenised file name for example for batch output of image assets.
To create a generator of all permutations, define a set of data for each of the tokens with the token name as the argument name:
for permutation in filename.resolve(colors=['white', 'black', 'red', 'blue']):
# do something with the permutation
print permutation.abspath
# prints
# /Users/demo/Desktop/assets/white/asset_white_1200px.jpg
# /Users/demo/Desktop/assets/black/asset_black_1200px.jpg
# /Users/demo/Desktop/assets/red/asset_red_1200px.jpg
# /Users/demo/Desktop/assets/blue/asset_blue_1200px.jpg
Once tokens have been replaced with real-world data to create permutations, the relevant folders can be created by typing:
permutation.make()
API Documentation¶
-
class
fptokens.
Filename
(root, folders=[], base=[], separator='_', extension='jpg', escape='$')[source]¶ Filenames with support for tokens.
Parameters: - root (str) – Root location
- folders (list of str) – Folder names, attribute supports tokens
- base (list of str) – Basename of the file, attribute supports tokens
- separator (str) – Separator for basename elements
- extension (str) – Filename extension
- escape (str) – Escape character for tokens, default:
$
-
abspath
¶ Return the filename’s full absolute path.
Returns: Absolute path Return type: Path
-
parse
()[source]¶ Parse the filename’s
folders
andbase
attributes, detect components that match the token pattern and replace these withfptokens.Token
objects.
-
resolve
(**kwargs)[source]¶ Given a set of **kwargs, yield all possible permutations for the data set provided. Raise
TokenError
ifFilename
does not haven tokens or the data provided does not match the tokens.Params **kwargs: Permutation data
-
root
¶ Return the filename’s root location.
Returns: Root location Return type: Path