Sublime Forum

ST3: ExportHtml

#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

#58

someone may define a keymap file not at the top level

Ah, I see why you wanted it now

0 Likes

#59

Yes, this is a very recent thing. Jon will be mentioning more on that pretty soon I’d imagine.

0 Likes

#60

Well, for this, I think you’d probably want it to default to return unicode text, with an option to get it binary.

In any case, the main thing is that it should be consistent.

A user of get_package_asset probably just wants something consistent, whether it be ST2/ST3, some partial contents from a zip, or complete contents from a file.

In sublime 2, the open() returns str() which is essentially the bytes() from python 3.

Sublime 3, with python 3, on the other hand, returns str() which is essentially unicode() from python 2.

zipfile seems to return raw bytes in python 2/3

0 Likes

#61

[pre=#0C1021]def _find_file(abs_dir, file_name):
for root, dirnames, filenames in os.walk(abs_dir):
if file_name in filenames:
return os.path.join(root, file_name)
[/pre]

I think you can boil down the code to pretty much that, which will return None implicitly if nothing is found.

0 Likes

#62

re: testing out the function, you can set up test suites which run only on reload as so:

[pre=#0C1021]##################################### TESTS ####################################

import unittest

class GetPackageAssetTests(unittest.TestCase):
def test_get_package_and_asset_name(self):
tc = get_package_and_asset_name
aseq = self.assertEquals

    r1 = (tc("Packages/Relative/one.py"))
    r2 = (tc("/Abs/Packages/ZipPseudo.sublime-package/nested/sort.py"))
    r3 = (tc(sublime.packages_path() + "/Absolute/Nested/asset.pth"))

    aseq(r1, ('Relative',   'one.py'))
    aseq(r2, ('ZipPseudo',  'nested/sort.py'))
    aseq(r3, ('Absolute',   'Nested/asset.pth'))

################ ONLY LOAD TESTS WHEN DEVELOPING NOT ON START UP ###############

try: times_module_has_been_reloaded += 1
except NameError: times_module_has_been_reloaded = 0 #reloaded

if times_module_has_been_reloaded:
target = name
suite = unittest.TestLoader().loadTestsFromName(target)

unittest.TextTestRunner(stream = sys.stdout,  verbosity=0).run(suite)

print ("running tests", target)
print ('\nReloads: %s' % times_module_has_been_reloaded)

################################################################################
[/pre]

0 Likes

#63

[quote=“castles_made_of_sand”]
I think you can boil down the code to pretty much that, which will return None implicitly if nothing is found.[/quote]

Nothing wrong with simplifying…but I can’t stand implicit Nones. It makes it so clear that that is actually the intent to actually return a None for clarity sake. I know this is stylistic and all…just saying…

0 Likes

#64

Fair enough :smile: Excuse the tasteless bike-shedding :blush:

I find it clearer in that particular case but to each their own

0 Likes

#65

This bike-shedding proven pretty constructive for me anyway, noticed some bugs in the gist code, and the edit preferences code.

I’m going to merge ideas/insights from both of them and slap some unit tests on em at some point.

0 Likes

#66

There seems to be a bug with path return. If path was not recursive, you would return the path as a boolean. I have fixed it below.

[pre=#2D2D2D]def get_package_asset(package_name, file_name, get_path=False, recursive_search=False):
packages_path = sublime.packages_path()
sublime_package = package_name + “.sublime-package”
path = None

if os.path.exists(os.path.join(packages_path, package_name)):
    if recursive_search:
        path = _find_file(os.path.join(packages_path, package_name), file_name)
    elif os.path.exists(os.path.join(packages_path, package_name, file_name)):
        path = os.path.join(packages_path, package_name, file_name)[/pre]
0 Likes

#67

bike-shedding, that was a new one for me; had to consult google :smile:. Yeah, I try not to get too invested in my stylistic opinions. Usually, I throw it out there and if people ignore it, I shrug it off. I realize my opinion doesn’t govern the world :smile:.

0 Likes

#68

github.com/sublimator/PackageRe … sources.py

My shed got purple :smile:

0 Likes

#69

[quote=“castles_made_of_sand”]https://github.com/sublimator/PackageResources/blob/master/package_resources.py

My shed got purple :smile:[/quote]

Cool. I will give it a try.

0 Likes

#70

Send us a pull request when you find bugs or add features eh …

The globber aint finished btw …

0 Likes

#71

[quote=“facelessuser”]I do do a number of score selectors, but not as many as you would think.

I grab the scope name from the selection and then compare all of the theme scopes with score_selector. Once I determine the best matches, I add the scope name with the best theme scope matches for foreground, background, etc. to the dictionary and never have to run score_selector on the same scope again.

I don’t notice much of a difference with the ST2 version and the ST3 version for this plugin.[/quote]

That’s a clever use of memoization :smiley:

0 Likes

#72

[quote=“facelessuser”]There seems to be a bug with path return. If path was not recursive, you would return the path as a boolean. I have fixed it below.
[/quote]

Woops, that was bad. :blush:

Is it possible to have assets outside of one of the sublime text paths (as specified in the following test)

r2 = (tc("/Abs/Packages/ZipPseudo.sublime-package/nested/sort.py"))

Made a couple updates. Used walk’s recursion rather than my own. get_package_and_asset_name should now

0 Likes

#73

Think I’ve taken everything that has been mentioned. get_package_asset now defaults to returning files UTF-8 encoded. In addition, there are two new parameters, a boolean to return the data in it’s binary form (default false) and a string for encoding type (default utf-8).

0 Likes

#74

[quote=“skuroda”]
Is it possible to have assets outside of one of the sublime text paths (as specified in the following test)

CODE: SELECT ALL
r2 = (tc("/Abs/Packages/ZipPseudo.sublime-package/nested/sort.py"))[/quote]

I don’t think so, not meaningfully anyway. If your routine relies on removing the install path prefixes just kill the test case.

In contrast to a path like Packages/Yes/no.yes

Abs there was just symbolic of an absolute path to the executable folder or installed_packages_path()

0 Likes