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

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

Related

Get absolute path to directory containing current 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).

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

Cannot call `require()` with absolute paths (`allow_url_include` is set to `0` in PHP.ini) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
error using include_once with a php variable
My code is like this:
<?php // file location: include/config.php
$CONFIG["DIRECTORY"]["HTMLINC"] = "http://127.0.0.1/mysite/html_include";
?>
,
<?php // file location: ./template.php
require_once("include/config.php");
// ...
require($CONFIG["DIRECTORY"]["HTMLINC"] . "/anotherpage.php"); // Doesn't work
?>
It gives the following warning message:
Warning: require(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in...
I'm currently testing the code in my local server, so I can easily change the value of allow_url_include. But I will eventually upload this code to a remote server. I cannot guaranty that allow_url_include will always be set to 1.
How do I prevent this warning message and make this code work?
that isn't an absolute path. that is a URL. the absolute path would be the path to the file on disk. If you go to /var/www/mysite/html_include to access the file to edit it, that is the absolute path. You would want to set $CONFIG["DIRECTORY"]["HTMLINC"] to that absolute path. You can use $_SERVER['DOCUMENT_ROOT'] to get the web root supplied by apache and append the /mysite/html_include to generate the right path. ideally you would probably have a separate config variable called something like APP_ROOT that is the document root + /mysite (the absolute path to your application). something like:
<?php
$CONFIG["DIRECTORY"]["APP_ROOT"] = $_SERVER['DOCUMENT_ROOT'].'/mysite';
$CONFIG["DIRECTORY"]["HTMLINC"] = $CONFIG["DIRECTORY"]["APP_ROOT"].'/html_include';
This would allow you to use APP_ROOT for other variables too:
$CONFIG["DIRECTORY"]["UPLOADS"] = $CONFIG["DIRECTORY"]["APP_ROOT"].'/uploads';
$CONFIG["DIRECTORY"]["IMAGES"] = $CONFIG["DIRECTORY"]["APP_ROOT"].'/images';
$CONFIG["DIRECTORY"]["THUMBNAILS"] = $CONFIG["DIRECTORY"]["APP_ROOT"].'/thumbs';
etc.
Don't use http URLs as your includes - they won't work the way you expect anyway.
If they are enabled, they will be treated as external URLs, and thus parsed by PHP before being served to the including page. The resulting output will therefore not include any PHP code because it will already have been executed.
If you want to include a local file, you need to provide a directory path on the local disk, not a URL (not even a localhost URL).
In any case, the ability to run includes from a URL is virtually always disabled by any sane hosting company because it is a massive security risk to allow code to come in from a third party site and be run directly on your system.
If you want to include a file local to the web site, you can use $_SERVER['DOCUMENT_ROOT'] to reference the root of the site, or __DIR__ to reference the directory of the current file. I guess the former would be best in your case, so you need this:
$CONFIG["DIRECTORY"]["HTMLINC"] = $_SERVER['DOCUMENT_ROOT']."/mysite/html_include";
hope that helps.
You put in a URL by using "http://127.0.0.1". You must reference it from the file system, not by local IP. So you need to set $CONFIG["DIRECTORY"]["HTMLINC"] = '/path/from/root/html_include'
"Absolute paths" in file systems are not like absolute paths in HTML (which begin with the protocol - i.e 'http://').

using an image embedded in a directory of a dll

I have a .NET 4.0 class library with a directory called Resources, with an image called Logo.bmp inside it set to be compiled as an embedded resource.
In my main application I add the dll reference and set a Uri to pack://application:,,,/ResourceImages;component/Resources/logo.bmp and then I try to get the resource stream to that resource (using Application.GetResourceStream(myUri)) but it can't find the resource specified.
If however I put the image in the root directory of my dll and take out the Resources/ it can find and return the resource stream without issue.
Any suggestions?
to anyone else who might be having this particular issue, make sure that you build the string to pass into the new uri BEFORE you make the new call, not during. I changed it so that the pack: location string is all created ahead of time and now it works

Resources