Get absolute path to directory containing current FreeMarker - freemarker

In Apache FreeMarker, how can I get the absolute path to the directory containing the current .ftl file?
For example, if I was processing the file /path/to/template.ftl, then I'm searching for a way to get /path/to inside of /path/to/template.ftl.
I've tried .current_template_name and friends, but these really only contain the name of the file, not its absolute path (from which I could get the parent directory). I've also tried absolute_template_name, but this just seems to prepend the name with a / to make the path seem absolute, but it does not resolve to the real absolute path.
Background: I'm templatizing Asciidoc files with with Freemarker, and the Asciidoc files must include other Asciidoc files which reside below the original directory of the .flt file, so they must not be searched relative to the temporarily "expanded" Asciidoc file.

The template paths that templates use are always virtual, and resolved by the TemplateLoader object set in the Configuration. TemplateLoader is just an interface, has multiple implementations, and so is a black box for FreeMarker. The actual location of the template can be inside a jar file, or even in the database table, so in general a template has no path on the file system.
Normally, you set up the TemplateLoader so that it can access all the templates you need. Then you don't need any tricks, and just use template paths.
Another possibility is to use a FileTemplateLoader that uses the root directory as base directory. This of course would be a bad idea for most applications (especially for web applications, for security reasons).

Related

Pillow in python: cannot open resource (.ttf) [duplicate]

I downloaded nltk data into the data directory in my Flask app. The views reside in a blueprint in another directory on the same level as the data directory. In the view I'm trying to set the path to the data, but it doesn't work.
nltk.data.path.append('../nltk_data/')
This doesn't work. If I use the whole path, it does work.
nltk.data.path.append('/home/username/myapp/app/nltk_data/')
Why does the first form not work? How can I refer to the location of the data correctly?
In Python (and most languages), where the code resides in a package is different than what the working directory is when running a program. All relative paths are relative to the current working directory, not the code file it's written in. So you would use the relative path nltk_data/ even from a blueprint, or you would use the absolute path and leave no ambiguity.
The root_path attribute on an app (or blueprint) refers to the package directory for the app (or blueprint). Join your relative path to that to get the absolute path.
resource_path = os.path.join(app.root_path, 'enltk_data')
There's probably no reason to be appending this folder every time you call a view. I'm not familiar with nltk specifically, but there's probably a way to structure this so you set up the data path once when you create your app.
project / app / blueprint
/ data
^ join with root_path to get here
^ app.root_path always points here, no matter where cwd is
^ current working directory

How can I change the file path of files referenced via the download role to not use a hashed folder?

I want to provide a link to a file in my project, but I want this link to be human readable and perma-ish.
Doing this:
Link to file for reference :download:`myfile.json <../myproject/myfile.json>`.
Generates a link that looks like this:
...../myproject/docs/_build/html/_downloads/b4c73f3851c188db23a20daeed2c/myfile.json
Do I have control over this? I want the link to just be this:
...../myproject/docs/_build/html/_downloads/myfile.json
I would actually prefer the link be in the root so it's just:
...../myproject/myfile.json
The download role does what it says in the documentation, i.e., it creates links with a unique hash. I don't see a way around it unless the implementation is changed.
But
I would actually prefer the link be in the root
In this particular case we can (ab)use html_extra_path, adding this in conf.py
html_extra_path = ['../myproject/myfile.json']
and refer to the file with a regular hyperlink:
Link to file for reference `myfile.json <myfile.json>`_.
The file is then necessarily in the root folder (of the built HTML documentation), as that's what html_extra_path does. It cannot be put in a subfolder such as _downloads.

How to provide relative path in Include controller in jmeter for test fragment

I have two thread groups from that in one thread I have two include controllers. but I want to provide relative path instead of absolute path so anyone can use this project in their system. as I am uploading this on GitHub.
enter image description here
If the external .jmx script lives at the same folder - just use its filename like Test Fragment.jmx in the Include Controller
If it resides under a folder - specify its relative location
somefolder/Test Fragment.jmx
For the parent folder:
../somefolder/Test Fragment.jmx
There is includecontroller.prefix property which is being added to the filename specified so if there is a common location for all external JMX files/test fragments you might want to set this property
More information:
Include Controller Documentation
Modularizing for Script Reusability: Examples in JMeter and YAML

Where to save application data in windows?

I am trying to make a windows application. In this application, some files get modified as a user add or delete an entry. I saved these files on the application folder itself.
But After making binary file I installed it, As I try to add a entry it get crashed.
So, I figured out the issue. The windows doesn't allow to modified files inside C:\Program Files.
So, I installed it in other drive and it works. It solved my issue temporarily but I want to know how other application works in windows.
Where do those applications save their data?
I am not talking about some data which get saved in "Documents" but something which is essential need to modified every time user makes change like theme, formates.
No user access is allowed to the "program folder", and that's for good: it is a system folder, and it should only be accessed for system related operations (like installing or uninstalling a program).
There are many places where "program data" can be stored depending on the situation, and QStandardPaths provides access to their paths, according to the category location. What you might be interested in are:
ConfigLocation: Returns a directory location where user-specific configuration files should be written. This may be either a generic value or application-specific, and the returned path is never empty.
AppDataLocation: Returns a directory location where persistent application data can be stored. This is an application-specific directory.
AppLocalDataLocation: As the previous one, but Windows specific.
AppConfigLocation: Returns a directory location where user-specific configuration files should be written. This is an application-specific directory, and the returned path is never empty.
Those paths (along with the others listed in the documentation) can be accessed using the following static methods:
standardLocations(locationType): returns a list of paths for the requested location type, in order of priority (the first is usually the preferred one);
writableLocation(locationType): returns the preferred path for which write access is allowed (usually the first of the standardLocations());
If you need to store the user configuration, you can use QStandardPaths.writableLocation(AppConfigLocation), while if you have some user-specific internal data that is used by the application (email database, document templates, etc) QStandardPaths.writableLocation(AppLocalDataLocation) should be a good choice.
In both cases, those paths may not exist, so you need to ensure that and eventually create them, possibly by using QDir(path):
dataPath = QtCore.QStandardPaths.writableLocation(AppLocalDataLocation)
dataPathDir = QtCore.QDir(dataPath)
if not dataPathDir.exists():
# create the directory (including parent directories if they don't exist);
# that the argument of mkpath is relative to the QDir's object path, so
# using '.' means that it will create the actual dataPath
dataPathDir.mkpath('.')
Note that for all of the above (especially the last 3) it's required that you correctly set both the organizationName and the applicationName.

pwd command returns "/" after shifting API to HTTPS [duplicate]

I downloaded nltk data into the data directory in my Flask app. The views reside in a blueprint in another directory on the same level as the data directory. In the view I'm trying to set the path to the data, but it doesn't work.
nltk.data.path.append('../nltk_data/')
This doesn't work. If I use the whole path, it does work.
nltk.data.path.append('/home/username/myapp/app/nltk_data/')
Why does the first form not work? How can I refer to the location of the data correctly?
In Python (and most languages), where the code resides in a package is different than what the working directory is when running a program. All relative paths are relative to the current working directory, not the code file it's written in. So you would use the relative path nltk_data/ even from a blueprint, or you would use the absolute path and leave no ambiguity.
The root_path attribute on an app (or blueprint) refers to the package directory for the app (or blueprint). Join your relative path to that to get the absolute path.
resource_path = os.path.join(app.root_path, 'enltk_data')
There's probably no reason to be appending this folder every time you call a view. I'm not familiar with nltk specifically, but there's probably a way to structure this so you set up the data path once when you create your app.
project / app / blueprint
/ data
^ join with root_path to get here
^ app.root_path always points here, no matter where cwd is
^ current working directory

Resources