Indenting issues for an existing script

57 views
Skip to first unread message

WoJ

unread,
Mar 26, 2013, 4:15:17 AM3/26/13
to pyscr...@googlegroups.com
Hello,

I wanted to move from Komodo to PyScripter and took an existing script. Unfortunately the automatic indenting fails, the indent is far by one space.
I use pure spacing (4 spaces per indent) and here is what happens:

The initial code:

    def handle_endtag(self, tag):

        if "h1" in tag:
            h1 = False

If I press enter at the end of the "if "h1" in tag:" line the cursor moves to column 11 instead of 12 (11 spaces instead of 12). I have "Tabs to spaces" checked, set the tab size to 4.

If I start a new script there are no issues.

Any ideas what to try next?

Thank you!

peter...@gmail.com

unread,
May 19, 2013, 3:23:52 AM5/19/13
to pyscr...@googlegroups.com
Hi,

I too got the indent problem when importing scripts with tabs not set to 4
spaces.

My solution: rewrite the script with correct indentation with this snippet:

<code>

import os, re, shutil
import pn

def _file(root, filename):
return os.path.join(root, filename)

root = r'E:\dev\python\wx scripts'
filename = 'exemple.py' # script with "wrong" indentation

filename = _file(root, filename)
# prevent overwriting backup file
assert not os.path.exists(filename+'.bak'), 'Backup file exists already.
What we do now?'

src = pn.file_get_contents(filename).splitlines()
new = []
first, indent = True, 0

for line in src:
try:
spaces, code = re.findall('^(\s+)(.*)', line)[0]
# the first indentation gives the number of whitespaces for each
indentation
if first:
indent = spaces.count(' ')
first = False
line = ' ' * (len(spaces)/indent * 4) + code
except IndexError:
if re.match('^\t.*', line):
line = re.sub('\t', '\s'*4, line) ## not tested but should work
finally:
new.append(line)

shutil.copy2(filename, filename+'.bak')
pn.file_put_contents(filename, '\n'.join(new))

</code>

Of course if you use indentation != 4 you'll change the 4 in the script.
I'll let it up to you to replace my pn.file_xxx_contents() functions to
anything that does the job of reading/writing to a file.

BTW, I'm a user of PyScripter.
> --
> You received this message because you are subscribed to the Google Groups
"PyScripter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to pyscripter+...@googlegroups.com.
> To post to this group, send email to pyscr...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pyscripter?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
Reply all
Reply to author
Forward
0 new messages