how to handle import of files using Jinja that don't exist - jinja2

In Salt Pillars I need to load the contents of a file into a Jinja variable. If the file doesn't exist Jinja throws an error (TemplateNotFound:...)
I have some folders (Company A, Company B, etc) in my Pillars that may or may not contain a file called Default.sls. If the file exists - it will be loaded into a variable. The problem arises when the file doesn't exist - in this case Jinja throws a TemplateNotFound exception.
My Pillar folder looks something like this:
pillar
|
|-Default.sls
|
|-Company A
| |-Default.sls <-- This could be missing
| |-init.sls
| |-prod.sls
| |-test.sls
| |-etc...
|
|-Company B
| |-Default.sls <-- This could be missing
| |-init.sls
| |-prod.sls
| |-test.sls
| |-etc...
|
|-Company C
|...
I have not found a good way to solve this. I have been using the following code (info: tpldir contains the name of the current foldername the sls file (in this case init.sls) is located in):
{{Company x}}/init.sls
----------------------
{% from tpldir + '/default.sls' import companyDefaultX with context %}
xx:
companyDefault: {{ companyDefaultX | yaml }}
In order to check if the file exists - I have been using:
salt['file.file_exists']('/srv/pillar/' + tpldir + "/default.sls")
This works fine as long as file_roots defined in Salt master config is setup as default. We have now moved to using gitfs - now the above use of file.file_exists obviously does not work since the files no longer is located on the salt master in /srv/pillar/...!
The reason for the above code is that it is a way of making a Tiering between default- and companyDefault configurations. First default (Defalt.sls) is read, then {{Company X}}/Default.sls is merged into/onto default. Thirdly environment (prod.sls, test.sls, etc) is merged with the new default...
Bonus question: How can I loop over files in a folder without using:
salt['file.readdir']('/srv/pillar/' + tpldir)
As this is the same problem (/srv/pillar)...

I have found a solution to the above. Only way is to use include in combination with "ignore missing":
{%- load_yaml as customerDefaultX %}
{% include tpldir + '/default.sls' ignore missing with context %}
{%- endload %}
The customerDefaultX variable will now contain the content of the {{Company X}}/default.sls file OR None if it doesn't exist...
Original bonus question is still open though ;-)

Related

MediaWiki different site title for different namespaces

I have found that there is a special MediaWiki page, MediaWiki:Pagetitle, in which I can set the site name to be displayed for all pages between <title> tags (default: $1 - Wiki's_name). I would like to have different messages depending on the namespace, for example:
for the main namespace a longer message: $1 - Wiki's_name, wiki's_subtitle
for files the default: $1 - Wiki's_name
This is because files usually have a longer name and everything becomes too long.
Is it possible to do this, perhaps with a condition, or doing this from another place rather than using MediaWiki:Pagetitle?
Set MediaWiki:Pagetitle to this:
{{#switch: {{NAMESPACENUMBER}}
| 6 = $1 - Wiki's_name
| $1 - Wiki's_name, wiki's_subtitle
}}
provided that ParserFunctions extension is installed.
Six is the number of File: namespace.

Mkdocs hyperlink not working in static pages

I'm trying to build a documentation with mkdocs.
The problem is that the links in the static created pages are not working.
Instead of going to [folder]/index.html I'm presented with the following page like in the following image
The problem however doesn't exist when i try mkdocs serve
Set the use_directory_urls setting to false in your mkdocs.yml config file:
use_directory_urls: false
The documentation explains:
This setting controls the style used for linking to pages within the
documentation.
The following table demonstrates how the URLs used on the site differ
when setting use_directory_urls to true or false.
Source file | Generated HTML | use_directory_urls: true | use_directory_urls: false
------------ | -------------------- | ------------------------ | ------------------------
index.md | index.html | / | /index.html
api-guide.md | api-guide/index.html | /api-guide/ | /api-guide/index.html
about.md | about/index.html | /about/ | /about/index.html
The default style of use_directory_urls: true creates more user
friendly URLs, and is usually what you'll want to use.
The alternate style can occasionally be useful if you want your
documentation to remain properly linked when opening pages directly
from the file system, because it create links that point directly to
the target file rather than the target
directory.
The last paragraph is the key to why this makes a difference.

Include data file as Assemble.io/YAML Front Matter variable

I have a base.hbs file that does a {{#foreach}} loop through some items in an array. That array comes from a YAML/FM variable, set on a per-page basis.
On some pages, that array could be huge, so I'd like to keep it stored in separate JSON file. Is it possible to set a YAML/FM variable to the data contained within a JSON file stored inside my configured data folder?
I've tried to do:
---
my-array: {{ my-data-file }}
---
...but with no success. It doesn't fail the build, but nothing is there.
Update
Alright, so messing around a bit more... turns out even though Assemble via Handlebars can recognize {{ my-data-file }}, YAML/FM doesn't like the dashes. It would fail with 'my' is not defined. So I changed the filename to use underscores, using:
---
my-array: <%= my_data_file %>
---
I'm keeping this up in case someone else runs into the same issue.

Compile, run and insert highlighted inline code

Is there any existing method of automatically compiling and running code highlights in jekyll pages, inserting the output into the target HTML?
I've made such a thing. Code repository is here : https://github.com/djacquel/JekyllDoc
The result is here : http://jekyll.pygmeeweb.com/tests/
How it works
A rake task generates a new page from a data file which contains code snipped
Data are like
snippet: '{% assign my_array = "one|two" | split: "|" %}{{ my_array | push: "three" }}'
This snippet is inserted in a template used to create a new page.
Then a Jekyll build will interpret the code in this new page.
Not so clear isn't it ?? Just ask I'll answer. New year lag...

PHPStorm exclude files by mask from indexing

I'm having a PHP project that for some reason (not my initiative) has some backup files like "somefile - backup.php".
How do I exclude "%backup%.php" from indexing?
Settings/Preferences | Editor | File Types | Ignore files and folders field on the bottom -- add *-backup.php pattern (or whatever pattern you need).
PLEASE NOTE: this affects all projects as it is an IDE-wide setting.
Alternatively (have not tried myself, but should work):
Settings/Preferences | Editor | File Types | Recognized File Types | Text
Add *-backup.php pattern there.
This will also affect all projects .. but instead of excluding it will treat them as plain text files, so no indexing/code completion/syntax highlighting/etc while still having such files in the Project View tree (so you can edit/delete/upload/etc them if necessary).
"Settings > File Types > Ignore file and folders" and add the path of the folder.
Example: *.lib;client/build;
This worked for me to ignore a folder.