I am new to turbogears and have an app that I am creating with multiple directories under public/media/ballots. I need to see if a directory exists.
path = "public/media/ballots/" + x['directoryName']
#return path
if os.path.exists(path):
listing = os.listdir(path)
i=0
for infile in listing:
#find path for ballot1, ballot2, etc from files in directory
i +=1
Ballot = "Ballot" +str(i)
x['Ballot']= infile
return x
else:
return "false"
I've tried multiple ways of defining the path: "/media/ballots/", "./media/ballots/", "media/ballots". But the os.path.exists(path) always returns false. I'm not sure how TG is accessing the public files.
tg.config['paths']['static_files'] will give you the absolute path of the public directory. You can then use os.path.join to chain it to the path of your files relative to the public one.
I had to make the path="projectname/public/media/ballots/"
Related
I created a simple DSL in Xtext. All my source files (".src") are in a set of folders under "src". I want the generated files (".obj") to be created inside "src-gen", but preserving the folder structure of the source files.
The below (default) code outputs all files in the same folder ("src-gen").
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
// Root node of parsing tree
val Program prg = resource.allContents.filter(Program).next()
// Compiled code
var String code = prg.compile().toString()
// change file extension
val outName = ....
// Generate output
fsa.generateFile(outName, code)
...
If file A.src is in folder src/one and B.src is in folder src/two I would like that A.obj to be created in src-gen/one and B.obj in src-gen/two.
You can use the resources getURI() to obtain the uri of the resource. then you can use that uri to find the segment with src and then take the following segments to get the path
I wanted to know how I can get the absolute path of a file which is inside my project not in the application running.
For example this one:
C:\Users\Mr\Documents\Example\org.xtext.example.mydsl\src\example.txt
I have tried with paths like:
val file = new File("relative path");
val absolutePathString = file.getAbsolutePath();
or
System.getProperty("user.dir");
But all of them retrieves me Eclipse's path and not my project's path.
Thanks for the help!
There does not need to be a file. Assuming you have a default eclipse with default filesystem and talk about a file in a project you may have a look a at these
public void doit(Resource r) {
URI uri = r.getURI();
if (uri.isPlatformResource()) {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true)));
file.getLocation();
file.getLocationURI();
file.getRawLocation();
file.getRawLocationURI();
}
}
I am new to both DOORS and DXL. I've been trying to copy a module in a project template to any given project folder using DXL, but my approaches haven't been working. Here's the part of my script where the copy and paste operations are attempted:
// Where string originalModule is the path to the module being copied.
// Where string targetPath is the path to where the copied module should be pasted.
ModName_ originalMMP = module(originalModule)
string originalMMPdesc = description(originalMMP)
clipCopy(originalMMP)
clipPaste(targetPath)
clipClear()
Whenever I run my script in the DOORS' DXL editor, I get an error indicating that the functions clipCopy() and clipPaste() have invalid arguments. In the DXL reference manual, it indicates that the type of the arguments should be of Item type, but I'm not totally sure I'm understanding that.
I have tried this other approach as well:
// The same conventions as above are used for the originalModule and targetPath
// string type variables.
// The variable string targetPathTemp contains the path to the replicated
// file New Module Temp
ModName_ originalMMP = module(originalModule)
string originalMMPdesc = description(originalMMP)
bool OK = copy(originalMMP,"New Module Temp", originalMMPdesc)
ModName_ newMMP = module(targetPathTemp)
// Moving and Renaming:
ErrMess = move(newMMP, targetPath)
ErrMess = rename(copiedMMP,newModuleName, originalMMPdesc)
I get the same errors as clipCopy() and clipPaste() for the functions: copy() and move().
Does anyone have any idea of what am I doing wrong, and what exactly am I not understanding?
Thanks in advance!
I think clipCopy and its brethren only work with Items. Use Item originalMMP = item(originalModule) instead of ModName_...
I have two packages: webserver and utils which provides assets to webserver.
The webserver needs access to static files inside utils. So I have this setup:
utils/
lib/
static.html
How can I access the static.html file in one of my dart scripts in webserver?
EDIT: What I tried so far, is to use mirrors to get the path of the library, and read it from there. The problem with that approach is, that if utils is included with package:, the url returned by currentMirrorSystem().findLibrary(#utils).uri is a package uri, that can't be transformed to an actual file entity.
Use the Resource class, a new class in Dart SDK 1.12.
Usage example:
var resource = new Resource('package:myapp/myfile.txt');
var contents = await resource.loadAsString();
print(contents);
This works on the VM, as of 1.12.
However, this doesn't directly address your need to get to the actual File entity, from a package: URI. Given the Resource class today, you'd have to route the bytes from loadAsString() into the HTTP server's Response object.
I tend to use Platform.script or mirrors to find the main package top folder (i.e. where pubspec.yaml is present) and find imported packages exported assets. I agree this is not a perfect solution but it works
import 'dart:io';
import 'package:path/path.dart';
String getProjectTopPath(String resolverPath) {
String dirPath = normalize(absolute(resolverPath));
while (true) {
// Find the project root path
if (new File(join(dirPath, "pubspec.yaml")).existsSync()) {
return dirPath;
}
String newDirPath = dirname(dirPath);
if (newDirPath == dirPath) {
throw new Exception("No project found for path '$resolverPath");
}
dirPath = newDirPath;
}
}
String getPackagesPath(String resolverPath) {
return join(getProjectTopPath(resolverPath), 'packages');
}
class _TestUtils {}
main(List<String> arguments) {
// User Platform.script - does not work in unit test
String currentScriptPath = Platform.script.toFilePath();
String packagesPath = getPackagesPath(currentScriptPath);
// Get your file using the package name and its relative path from the lib folder
String filePath = join(packagesPath, "utils", "static.html");
print(filePath);
// use mirror to find this file path
String thisFilePath = (reflectClass(_TestUtils).owner as LibraryMirror).uri.toString();
packagesPath = getPackagesPath(thisFilePath);
filePath = join(packagesPath, "utils", "static.html");
print(filePath);
}
To note that since recently Platform.script is not reliable in unit test when using the new test package so you might use the mirror tricks that I propose above and explained here: https://github.com/dart-lang/test/issues/110
public string AsyncUpload()
{
return _fileStore.SaveUploadedFile(Request.Files[0]);
}
in the SaveUpload
private string _uploadsOriginalFolder = HostingEnvironment.MapPath("~/Content/Images/OriginalImages");
private string _uploadsThumbnailFolder = HostingEnvironment.MapPath("~/Content/Images/Thumbnails");
var identifier = Guid.NewGuid();
var originalFileNameToSave = identifier.ToString() +".jpg" ;
fileBase.SaveAs( Path.Combine(_uploadsOriginalFolder, originalFileNameToSave));
imagingService.ResizeImage(Path.Combine(_uploadsOriginalFolder, originalFileNameToSave),
Path.Combine(_uploadsThumbnailFolder,originalFileNameToSave),
160,
120,
false);
return originalFileNameToSave;
I want to have Domain.Com/Thumbnails/guid.jpg and Domain/OriginalImages/guid.jpg
i want this to be stored on to a \dfs.domain.com\Scanning[project]\Images directory.
how do i save it to that location and what rights do i need to provide on it.
has any body done this before. i guess many?
You can store the image wherever you want on your server as long as you provide write permissions to that folder. Obviously if you want to access it from another ASP.NET application you can no longer use relative paths. So if the dfs.domain.com site root is physically mapped toc:\foobar you could save the file as absolute path at c:\foobar\Scanning[project]\Images folder. The MVC application that is saving the file has no way of knowing how is this other site configured so you could externalize the other site root path at your web.config.
The best is to map phisical path to virtual folder in iis, and then allow write permisson on it. This is how I done when I have multiple web apps, which are using same folder for static content.