Jeremy Chen
Jeremy Chen

Reputation: 1347

What is this "join" doing?

I just installed retext via pip. I have to download icons for it but I realise it doesn't work (no icons on the menu) unless i run "retext" in the folder of retext.

I tried to fix it but my python skills are not very strong.

At the moment, I force the icon_path to have the path I want.

#icon_path = 'icons/'
icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'

Can someone enlighten me how this line works?

datadirs = [join(d, 'retext') for d in datadirs]

Thanks.

import sys
import markups
import markups.common
from os.path import dirname, exists, join

from PyQt5.QtCore import QByteArray, QLocale, QSettings, QStandardPaths
from PyQt5.QtGui import QFont

app_version = "6.0.1"

settings = QSettings('ReText project', 'ReText')

if not str(settings.fileName()).endswith('.conf'):
        # We are on Windows probably
        settings = QSettings(QSettings.IniFormat, QSettings.UserScope,
                'ReText project', 'ReText')

datadirs = QStandardPaths.standardLocations(QStandardPaths.GenericDataLocation)
datadirs = [join(d, 'retext') for d in datadirs]

if sys.platform == "win32":
        # Windows compatibility: Add "PythonXXX\share\" path
        datadirs.append(join(dirname(sys.executable), 'share', 'retext'))

if '__file__' in locals():
        datadirs = [dirname(dirname(__file__))] + datadirs

#icon_path = 'icons/'
icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'
for dir in datadirs:
        if exists(join(dir, 'icons')):
                icon_path = join(dir, 'icons/')
                break

Upvotes: 1

Views: 89

Answers (1)

Will
Will

Reputation: 24699

This is os.path.join():

os.path.join(path, *paths)

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

It is imported here:

from os.path import dirname, exists, join

So, the line in question:

datadirs = [join(d, 'retext') for d in datadirs]

The [...] is a List Comprehension that builds a list of the results of join(d, 'retext') applied to each directory in the datadirs list.

So, if datadirs contained:

['/usr/local/test', '/usr/local/testing', '/usr/local/tester']

Then:

[join(d, 'retext') for d in datadirs]

Would produce:

['/usr/local/test/retext', '/usr/local/testing/retext', '/usr/local/tester/retext']

The problem with setting:

icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'

Is that it's being overwritten in the for loop, so unless a proper path is not found, it will be overwritten.

Upvotes: 1

Related Questions