Sublime Forum

ST3: ExportHtml

#38

Letā€™s pretend that I did that originally :smile: I did it in the _search_zip method. Also added a method to get package and asset name from absolute path. Updates on the Gist .

Edit: err half way. Woops. :blush:

0 Likes

#39

Another thing, generally you wanna use try/finally when using open() zipfile.ZipFile() etc

Python has with for that: with open(bla) as file_handle: file_handle.read()

0 Likes

#40

def get_package_and_asset(path): package = os.path.basename(os.path.dirname(path)) package = package.replace(".sublime-package", "") asset = os.path.basename(path) return (package, asset)

I want my money back! Oh wait, I didnā€™t pay any nor do I really care about windows absolute paths!

0 Likes

#41

Or will that work on NT and Iā€™m loopy?

0 Likes

#42

[quote=ā€œcastles_made_of_sandā€]def get_package_and_asset(path): package = os.path.basename(os.path.dirname(path)) package = package.replace(".sublime-package", "") asset = os.path.basename(path) return (package, asset)

I want my money back! Oh wait, I didnā€™t pay any nor do I really care about windows absolute paths![/quote]

Looks at my poor poor windows machine that I am forced to use for development :frowning:

0 Likes

#43

Nevermind :smile: Iā€™m satiated haha

os.path.basename

Thatā€™s the one I was trying to think of the other day

0 Likes

#44

I think itā€™ll be fine actually

0 Likes

#45

[code]>>> import posixpath

import ntpath
ntpath.basename(ā€œC:\fuck\fuckerā€)
ā€˜fuckerā€™

os.path is posixpath
True
[/code]

Learn something every day

0 Likes

#46

Yup I tested it after I posted. Also added the ā€œwithā€ statement. Hadnā€™t seen that before, though I still should have done the try catch stuff. Oh well though, this is why itā€™s good to share code.

0 Likes

#47

Oh, wait, one more thing.

Will that helper handle nested assets? ā€œPackages/Path/Subfolder/asset.pthā€?

0 Likes

#48

It will as soon as I fix it?

edit:
the ā€œtooā€ at the end was wrong and bothering me.

0 Likes

#49

Haha, good answer

Thereā€™s also the bytes/unicode inconsistency on python 3 between zipfile open and normal open

0 Likes

#50

Had to go take care of some other things, so that took longer than I wanted it to. Anyways, it should now handle nested assets. Also, everything is returned as bytes now.

0 Likes

#51

I meant more this guy:

def get_package_and_asset(path): package = os.path.basename(os.path.dirname(path)) package = package.replace(".sublime-package", "") asset = os.path.basename(path) return (package, asset)

That helper doesnā€™t handle nested paths too well.

Your get_package_asset function should have worked with nested files before, if Iā€™m not mistaken :smile:

0 Likes

#52

Ah I see. I willā€¦get back on that. Ha. No I donā€™t think it would. I was just looking at the top level package directory, so anything nested would have been missed. I wasā€¦ummā€¦testing you to make sure you saw the problem too? Yea letā€™s go with that :smiley:

0 Likes

#53

Unless you meant specifying the entire asset path, then yes it would have. In fact, maybe I should go back to that.

Edit:
So get_package_and_asset_name handles recursive files. Iā€™ve added a parameter to get_package_asset to do the recursive search (defaults to false). I was thinking this may be useful if nested folders are allowed for settings. I think Iā€™ve seen a post on this forum related to something like that.

0 Likes

#54

Sorry, was out getting dinner. So it will search for a given base file name anywhere in a zip/folder?

Canā€™t imagine when Iā€™d want that really.

Mostly I just want ability to get things like dict(pkg='Murky', relative_path='waters/salty/green.color')

Will look over latest incarnation

0 Likes

#55

gist.github.com/skuroda/4965913 ā€¦ set-py-L40

Opening explicitly in rb mode ftw

0 Likes

#56

gist.github.com/skuroda/4965913 ā€¦ et-py-L125

os.walk should in essence recurse by itself I think

I know the doc for get_package_and_asset_name says ā€œThis method will return
the package name and asset name from an absolute pathā€ but it would be nice if
it was able to handle paths as returned by things like:

>>> view.settings().get('syntax') 'Packages/Python/Python.tmLanguage'

0 Likes

#57

It should do what you want (though test it to make sure). I did some and it appears to extract the package and path properly. The recursive thing was more of a me thinking of something else when you said nested files. Though if sublime text allows for nested directories, someone may define a keymap file not at the top level. Of course, this doesnā€™t affect your stuff. I may go back and just take it out. If Sublime Text allows the nested structure at some point, I can re add it. With that being said, Iā€™m done for tonight. Iā€™ll check back tomorrow for feedback.

Ha yup. Do you know of any reason not to do that? Iā€™ve never had a reason to open files as binary in python, so Iā€™m not sure itā€™s 100% safe.

Hmm, I didnā€™t think it did when I test it, or maybe I just wasnā€™t paying attention. And yes I wasnā€™t paying attention. Sadā€¦maybe thatā€™s fatigue setting in.

Side notes:
Hmm, great how export html was only discussed on sayā€¦the first 20 posts in this thread, but itā€™s all related. And as a random noteā€¦apparently the Sublime Text team is at least 2 people (the latest blog post isnā€™t from Jon).

0 Likes